6

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?

Community
  • 1
  • 1
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 3
    See here: http://stackoverflow.com/questions/6202041/php-memory-get-peak-usagefalse-when-should-i-use-true – user1578653 Sep 19 '13 at 15:44
  • @user1578653 Indeed that's a dup, thanks for the pointer. Any better answer would be appreciated, though. – BenMorel Sep 19 '13 at 15:49
  • How about this one, it has a slightly more useful answer: http://stackoverflow.com/questions/15745385/memory-get-peak-usage-with-real-usage – user1578653 Sep 19 '13 at 15:52

1 Answers1

3

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322