Log memory usage using declare and ticks in PHP
Update:See the first comment, looks a far better and more accurate way of tracking memory usage, thanks Mathieu.
As far as I know, there isn’t any memory footprint profiling in Xdebug, I think there was at some point but they removed it because it was a little flaky. I like to monitor the memory usage within my scripts, and I’ve found this simple snippet can help. I don’t think it’s entirely accurate, but it can help a little. Unfortunately, I don’t think it works all that well on Windows, if at all. All good on Linux though.
<?php
function log_memory() {
$_SESSION['memory'][] = array(
'time' => microtime(true),
'memory' => memory_get_usage(),
); // or insert your logging code here
}
declare(ticks = 50); // log every 50 ticks (low level instructions)
register_tick_function('log_memory');
$var = 'a';
for($i=0;$i<100;$i++) {
$var .= 'a';
}
var_dump($_SESSION['memory']);
?>
Tags: PHP, scalability








May 14th, 2008 at 3:53 pm
When you use an xdebug trace instead of profiling you’ll get the memory usage per entry in the trace.
Tad more work to go through, but probably more reliable than ticks..
http://xdebug.org/docs/execution_trace
May 14th, 2008 at 5:09 pm
Thanks Mathieu, I’ve updated the post to recommend your comment.
May 14th, 2008 at 5:49 pm
[...] Bundle – May 14 Author : Jonathan Franzone No Comments Log memory usage using declare and ticks in PHP Dave Marshall, a web developer living in Hull, England, share a really quick and simple trick to [...]
May 14th, 2008 at 8:54 pm
Watch out, register_tick_function will crashed on threaded servers. Also I haven’t been able to get it to work in a windows environment yet.
May 16th, 2008 at 4:52 pm
Very timely article. Thank you. I was debugging a large online store that was using 25+MB of memory on the first load. I modified it slightly to not use sessions, and also show which file and what line of the file was being run at each tick:
$memory_usage = array();
$start_time = microtime(true);
function log_memory(){
global $memory_usage;
global $start_time;
$stacktrace = debug_backtrace();
$memory_usage[] = array(
‘time’ => microtime(true) – $start_time,
‘memory’ => memory_get_usage(),
‘file’ => $stacktrace[1]['file'] . ‘:’ . $stacktrace[1]['line']
);
}
declare(ticks = 100);
register_tick_function(’log_memory’);
May 19th, 2008 at 10:55 am
[...] Marshall posted article about php script memory usage logging. Memory logging is important because you may know haw many [...]
May 20th, 2008 at 4:45 am
You’re better off logging the backtrace etc to a file, otherwise you are using a global variable which takes up memory itself.
<?php
echo “at line ” . __LINE__ . ” memory is ” . memory_get_usage(true) . “\n”;
$log = array();
for ($i = 0; $i microtime(true),
‘memory’ => memory_get_usage(true),
‘file’ => __FILE__
);
}
echo “at line ” . __LINE__ . ” memory is ” . memory_get_usage(true) . “\n”;
May 20th, 2008 at 8:34 am
@Chris – Not really sure I follow you’re example. I agree logging to a file would be better for accuracy, but would slow your application down greatly, hence the ‘insert your logging code here’.
May 26th, 2008 at 2:02 am
Hi Dave,
My example was to show that if you keep the memory usage in an array (which is stored *in memory*) – you get wrong results.
April 20th, 2009 at 12:25 am
You could always load into a shm or var cache like XCache
June 16th, 2009 at 10:37 pm
Check out: http://particletree.com/features/php-quick-profiler/
Should meet most of the needs discussed in this post.
September 2nd, 2009 at 3:55 am
Nice work on that one Mathieu!