开发者

Difference between memory_get_peak_usage and actual php process' memory usage

开发者 https://www.devze.com 2023-04-02 00:29 出处:网络
Why result of php memory_get_peak_usage differs so much from memory size that is shown as allocated to process when using \'top\' or \'ps\' commands in Linux?

Why result of php memory_get_peak_usage differs so much from memory size that is shown as allocated to process when using 'top' or 'ps' commands in Linux?

I've set 2 Mb of memory_limit in php.ini My single-string php-script with

echo memory_get_peak_usage(true);

says that it is using 786432 bytes (768 Kb)

If I try to ask system about current php process

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

it gives me

F S 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 that 28 Mb come from and is there any way to decrease memory size that is being used by php-fpm process?


The memory size is mostly used by the PHP process itself. memory_get_peak_usage() returns the memory used by your specific script. Ways to reduce the memory overhead is to remove the number of extensions, statically compile PHP, etc.. But don't forget that php-fpm (should) fork and that a lot of the memory usage that's not different between PHP process is in fact shared (until it changes).


PHP itself may only be set to a 2meg limit, but it's running WITHIN a Apache child process, and that process will have a much higher memory footprint.

If you were running the script from the command line, you'd get memory usage of PHP by itself, as it's not wrapped within Apache and is running on its own.


The peak memory usage is for the current script only.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号