Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does memory_get_peak_usage(true) do? [duplicate]

Tags:

php

The PHP manual says:

int memory_get_peak_usage ([ bool $real_usage = false ] )

Returns the peak of memory, in bytes, that's been allocated to your PHP script.

Parameters

real_usage

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.

So how is emalloc() not a real usage, and how does TRUE compute the real memory usage then?

This question on StackOverflow asks the same thing, but the only answer doesn't dive into details about how computation is done, apart from rounding some allocations to the next kilobyte.

Any more extensive answer on what's happening under the hood when you use FALSE and TRUE?

like image 671
BenMorel Avatar asked Sep 19 '13 15:09

BenMorel


1 Answers

This question is a dup, as said above.

However, I think I should summarize my understanding from the different answers and comments:

  • memory_get_peak_usage(false) returns the exact memory used by the PHP script. Use to compare exact memory consumption of different sections of a PHP script.
  • memory_get_peak_usage(true) returns the memory allocated from the system to the PHP script, it's always higher because the Zend engine allocates the memory in 256KB chunks. Use to know the real impact of a given PHP script on the system.

So basically, memory_get_peak_usage(true) should be memory_get_peak_usage(false) rounded to the next 256KB.

like image 125
BenMorel Avatar answered Sep 21 '22 13:09

BenMorel