Difference between memory_get_peak_usage and actual php process' memory usage

左心房为你撑大大i 提交于 2020-01-02 04:13:30

问题


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?


回答1:


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).




回答2:


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.




回答3:


The peak memory usage is for the current script only.



来源:https://stackoverflow.com/questions/7273517/difference-between-memory-get-peak-usage-and-actual-php-process-memory-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!