Is the memory allocated by PHP a single request always issued at the end?

I am a bit confused about a memory leak in PHP.

I read that PHP automatically frees up the memory used in each request thanks to 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) related to PHP and memory leaks.

So I feel like I'm losing something.

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

+4
source share
1 answer

It is not possible to have memory leaks from PHP scripts between different requests (when using the Apache default configuration), because the variables and code used in the same request are freed at the end of this request, and the PHP memory allocator is restarted for the next request. However, errors in the interpreter or PHP extensions may leak memory separately.

A much bigger problem is that Apache child processes have PHP memory space inside them. They swell to allocate peak memory usage of the PHP script, and then keep this memory allocation until the child process is killed (as soon as the process asks the kernel to allocate part of the memory, this memory will not be released until the process dies). For a more detailed explanation of why this is a problem and how to deal with it, see My answer to Server Error .

A memory leak in a script where variables are not disconnected and the PHP garbage collector fails is very rare - most PHP scripts run for several hundred milliseconds, and this is usually not enough for even a serious memory leak to occur.

You can control how much memory your PHP script uses with memory_get_usage() and memory_get_peak_usage() - there is also a good explanation of memory usage and protection methods in the PHP manual .

PHP memory management is described in detail in this article .

edit: You can define compiled modules in Apache using httpd -l - the default values ​​depend on the OS configuration and repository configuration. There are many ways PHP can interact with Apache - the most detailed here .

+7
source

All Articles