Measure memory usage in PHP

Hopefully this explains a simple method of measuring memory usage in PHP.

$time = microtime(TRUE);
$mem = memory_get_usage();

/** 
 * Put the code here that you want to measure
**/

print_r(array(
  'memory' => (memory_get_usage() - $mem) / (1024 * 1024),
  'seconds' => microtime(TRUE) - $time
));

Here’s an example, where I sleep the script for ~ 10 seconds.

And how it looks in the browser: (I’ve added <pre> around it for easy reading)

You can find more examples and full documentation on the http://php.net/memory_get_usage web site.


Posted

in

by

Tags: