modperl has a nice feature.
PerlRequire conf/modperl.pl
PerlCleanupHandler Apache2::SizeLimit
and here is the script preload that handles it (modperl.pl):
use Apache2::SizeLimit;
$Apache2::SizeLimit::MAX_PROCESS_SIZE = 150000;
$Apache2::SizeLimit::MAX_UNSHARED_SIZE = 125000;
$Apache2::SizeLimit::CHECK_EVERY_N_REQUESTS = 5;
1;
It checks its own size and kills itself nicely before any more is served from it. It really has saved from runaway webservers... and other issues.
I don't know if PHP has a function similar, it would sure fix your issue lickety split.