Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between memory_get_peak_usage and actual php process' memory usage

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?

like image 772
Miroshko Avatar asked Sep 01 '11 16:09

Miroshko


1 Answers

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

like image 65
Evert Avatar answered Sep 20 '22 03:09

Evert