7

I'm a bit confused about the memory leaks in PHP.

I've read that PHP is releasing automatically the memory used in each request thanks to the Zend Memory Manager: http://www.webreference.com/programming/php_mem/2.html

But I see a lot of people and topics (even here in SO) concerned about PHP and memory leaks.

So I feel that I'm losing something.

Is it possible to have memory leaks in PHP between different requests?

1 Answer 1

8

It is not possible to have memory leaks from PHP scripts between different requests (when using the default Apache configuration), as the variables and code used in one request are released at the end of that request and PHP's memory allocator starts afresh for the next request. Bugs in the PHP interpreter or extensions could leak memory separately, however.

A much greater problem is that Apache child processes have PHP's memory space inside them. They swell to allocate the peak memory usage of a PHP script and then maintain this memory allocation until the child process is killed (once a process has asked the kernel to allocate a portion of memory, that memory won't be released until the process dies). For a more detailed explaination of why this is a problem and how to combat it, see my answer on Server Fault.

Memory leaks in a script, where variables are not unset and the PHP garbage collector fails, are very rare - most PHP scripts run for a few hundred milliseconds, and this is not generally enough time for even a serious memory leak to manifest.

You can monitor how much memory your PHP script is using with memory_get_usage() and memory_get_peak_usage() - there is also a good explanation on memory usage and how to program defensively in the PHP manual.

PHP's memory management is explained in detail in this article.

edit: You can determine the compiled in modules to Apache with httpd -l - defaults vary by OS distribution and repository configuration. There are plenty of ways to interface PHP to Apache - most detailed here.

Sign up to request clarification or add additional context in comments.

8 Comments

how can I check if Apache is using prefork mode? is that the default option? why anyone should not use that (I mean what are the cons of using prefork mode)?
@Andy Apache doesn't creates a new process for every request. Processes are actually reused a certain number of times (for performance reasons). See MaxRequestsPerChild.
@arnaud576875 that directive is in relation to Apache child processes, not PHP processes
@Andy PHP itself doesn't creates processes (unless you are using FastCGI or php-fpm, but this doesn't appear to be the case as you are speaking of Apache forks.) PHP as an Apache module is just code that lives in Apache processes.
This is still wrong, Apache doesn't manage PHP's memory. If PHP does a malloc() and no free() during a request, the allocated memory is leaked for the lifetime of the Apache process.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.