Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memory_get_usage(false) vs memory_get_usage(true)

Can somebody clarify what's the exact difference of the $real_usage parameter for memory_get_usage()?

The manual states:

Set this to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is reported.

But what's the difference? Which value is comparable to the memory_limit INI setting?

For some long running scripts I get something like 250MB real usage and just 50MB emalloc usage. How is that possible?

I'm trying to analyze some memory leak problems and memory_get_usage() is an important indicator for me that I need to fully understand. Thanks.

like image 849
Udo G Avatar asked Jul 10 '13 09:07

Udo G


1 Answers

The real memory is the amount of memory that's reserved for the PHP process. PHP reserves memory in large chunks, and this will be <= memory_limit, and may increase during the script's execution. Eg. If it fills up a chunk, and you then try to assign to a new variable, it will reserve another chunk.

If you set the parameter to FALSE, it only reports the amount of memory that PHP is actually using inside the reserved memory. This will increase every time you assign to a new variable, or call a function etc.

If you're interested in the amount of memory that is actually being used, then you'll want the real memory.

If you're debugging, or trying to tune a piece of code, you'll probably be more interested in the emalloc() memory. This would allow you to check the memory before and after an assignment or function call and calculate the delta.

like image 127
Luke Mills Avatar answered Oct 18 '22 11:10

Luke Mills