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']);

?>

Popularity: 46% [?]

Tags: ,


9 Responses to “Log memory usage using declare and ticks in PHP”

  1. Mathieu Kooiman Says:

    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

  2. daveyboy Says:

    Thanks Mathieu, I’ve updated the post to recommend your comment.

  3. Link Bundle - May 14 | franzone.com Says:

    [...] 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 [...]

  4. EllisGL Says:

    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.

  5. Konr Ness Says:

    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’);

  6. Logging PHP script memory usage | PHP Readings Says:

    [...] Marshall posted article about php script memory usage logging. Memory logging is important because you may know haw many [...]

  7. Chris Says:

    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”;

  8. daveyboy Says:

    @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’.

  9. Chris Says:

    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.

Leave a Reply