3

I was profiling an Ajax call with xDebug when I came across the PHP function memory_get_usage. It was a good function to confirm that I had reduced memory usage but I was curious about how it works.

There is a similar questions to my question but I believe it fails to properly explain what I'm asking and has no answers: memory_get_usage() real_size is less than allocated size

I added the functions at the very end of my php script, right before json is returned like so:

  ...
  $this->body = array(
    'node'  => $runtime->$method(intval($vertex), $options),
    'user'  => $runtime->commit(),
    'menu'  => $runtime->personalizer->getMenu(),
    'memory' => memory_get_usage(),
    'real_memory' => memory_get_usage(true),
    'memory_formatted' => $this->formatBytes(memory_get_usage()),
    'real_memory_formatted' => $this->formatBytes(memory_get_usage(true))
  );
}

function formatBytes($bytes, $precision = 2) {
  // See https://stackoverflow.com/a/2510459/1260548
  ...
}

I am running this in a Vagrant machine running Ubuntu 18.04 and PHP 7.2. What made me curious is that the real memory usage is always lower than the non-real output.

memory is 9984584 bytes while real_memory is 2097152 bytes

Why does memory_get_usage() return a lower value when the $real_usage parameter is set to true?

Uberswe
  • 1,038
  • 2
  • 16
  • 36

0 Answers0