PHP memory auto release after certain time

apache-2.2httpmemoryPHP

We have a web server running a PHP application to serve customers.

  • PHP 4
  • 3 GB memory
  • Centos
  • Apache HTTP server.

As our customer load has increased, we are having server memory issues. Sooner or later, we need to restart the HTTP service to release the memory (probably 5 times a day).

We can tell from top that most of the HTTP processes are fine, only using a little memory, but there are about 3-4 HTTP processes that are taking 80% of the memory and almost zero CPU usage. It looks like dead processes, but they don't release memory until we restart the HTTP service.

We suspect some part of our PHP code isn't working well and causing huge memory usage, then hanging there. Is there a PHP setting somewhere we can set that will terminate the process and free up the memory after, say, 5 minutes idle? This is a short term solution, we will have to find out what code is doing this — hard to find, though.

Best Answer

There are a couple lines in your php.ini file that are relevant. One is the memory_limit which defines how much RAM any given php script is allowed to eat through, the other is max_execution_time which defines how much CPU time it gets before being killed off. Be aware that this latter time is based on CPU time not clock time, so if it isn't using any CPU it might not ever add up.

Also as you noted these are temporary hacks to mitigate a mis-behaving app, not a real fix. You might find strace useful to figure out exactly what the memory-hogging processes are up to.