Difference between memory usage by memory_get_peak_usage and actual usage of php process

Why is the result of php memory_get_peak_usage very different from the size of the memory, which is shown as dedicated for processing when using the "top" or "ps" commands on Linux?

I installed 2 Mb memory_limit in php.ini My single line php script with

echo memory_get_peak_usage(true); 

says it uses 786432 bytes (768 Kb)

If I try to ask the system about the current php process

 echo shell_exec('ps -p '.getmypid().' -Fl'); 

it gives me

 FS UID PID PPID C PRI NI ADDR SZ WCHAN RSS PSR STIME TTY TIME CMD 5 S www-data 14599 14593 0 80 0 - 51322 pipe_w 6976 2 18:53 ? 00:00:00 php-fpm: pool www 

RSS param is 6976, so memory usage is 6976 * 4096 = 28573696 = ~ 28 MB

Where does this 28 MB come from and is there a way to reduce the memory size used by the php-fpm process?

+4
source share
3 answers

The memory size is mainly used by the PHP process itself. memory_get_peak_usage() returns the memory used by your particular script. Ways to reduce memory overhead are to remove the number of extensions, statically compile PHP, etc. But don't forget that php-fpm (should) fork and that most of the memory usage that is no different between a PHP process is a common fact (until it changes).

+5
source

Only the limit of 2 million can be set in PHP itself, but it works WITHIN in the Apache child process, and this process will have a much larger amount of memory.

If you ran the script from the command line, you could use the PHP memory yourself, since it was not completed in Apache and works on its own.

+1
source

Peak memory is used only for the current script.

0
source

All Articles