I found out a solution for Apache 2. However this works only without threads and only on POSIX compatible OS systems (e.g. Linux, OpenSolaris...).
<?php
// Terminate Apache 2 child process after request has been
// done by sending a SIGWINCH POSIX signal (28).
function kill_on_exit() {
posix_kill( getmypid(), 28 );
}
register_shutdown_function( 'kill_on_exit' );
?>
apache_child_terminate
(PHP 4 >= 4.0.5, PHP 5)
apache_child_terminate — Beendet einen Apacheprozess nach der Anfrage
Beschreibung
apache_child_terminate() registriert den Apacheprozess, der die aktuelle PHP-Anfrage ausführt, um ihn nach vollständiger Beendigung des PHP-Prozesses zu terminieren. Die Funktion kann verwendet werden, um einen Prozess zu terminieren, nachdem ein Skript mit hohem Speicherverbrauch gelaufen ist, der normalerweise nur intern wieder freigegeben wird, aber nicht wieder für das Betriebsystem freigegeben wird.
Rückgabewerte
Gibt TRUE zurück, wenn PHP als Modul unter Apache 1 läuft, die Apacheversion
nicht multithreaded läuft und die PHP-Direktive child_terminate eingeschaltet ist
(diese ist standardmäßig ausgeschaltet). Wenn diese Bedingungen nicht
zutreffen, wird FALSE zurückgegeben und ein Fehler vom Typ
E_WARNING generiert.
Changelog
| Version | Beschreibung |
|---|---|
| 5.4.0 | Diese Funktion wurde unter FastCGI verfügbar. Vorher wurde sie nur unterstützt, wenn PHP als Apache Modul installiert war. |
Anmerkungen
Hinweis: Diese Funktion ist auf Windows-Plattformen nicht implementiert.
Apache child processes are greedy. If they get bloated by a PHP application that requires a lot of memory, they stay that way. The memory is never given back to the OS until that child dies.
You could use MaxRequestsPerChild in Apache to kill all child processes automatically after a certain number of connections. Or you can use apache_child_terminate to kill the child after your memory intensive functions.
Note: apache_child_terminate is not available in Apache 2.0 handler.
In response to sam at liddicott dot com:
it isin't so simple! You should never kill an apache process because it is automatically freed when apache need!
And, if you use apache worker or thread based mpm you risk to kill the entire process!
result: DO NOT USE THIS FUNCTION!
this code will add apache_child_terminate() function if it is not already present.
if (!function_exists("apache_child_terminate")){
function apache_child_terminate(){
register_shutdown_function("killonexit");
}
function killonexit(){
@exec("kill ".getmypid());
}
}
