views:

1605

answers:

6

Is there anything out there freeware or commercial that can facilitate analysis of memory usage by a PHP application? I know xdebug can produce trace files that shows memory usage by function call but without a graphical tool the data is hard to interpret.

Ideally I would like to be able to view not only total memory usage but also what objects are on the heap and who references them similar to Jprofiler.

A: 

A graphical tool for xdebug output is KCacheGrind.

HabarNam
I am aware of KCacheGrind and WinCacheGrind. Neither tool allows analysis of memory usage as far as I can tell.
Oleg Barshay
+1  A: 

Try webgrind. It gives you the profiling of CacheGrinder in an easy to read, browser based format. I'm on a Mac and it has made profiling a breeze.

gaoshan88
Unfortunately webgrind does not report memory usage -- just execution speed.
Oleg Barshay
A: 

phpDesigner 2008 can debug and benchmark websites using xdebug and KCacheGrind. It also has a built-in monitor.

Does it handle memory usage profiling or just performance profiling? From their website I only saw performance profiling.
Oleg Barshay
Couldn't tell you. The programmer of this program is a very nice guy. Wouldn't hurt to shoot him an email and ask yourself!
+6  A: 

I came across the same issue recently, couldn't find any specific tools unfortunately.

But something that helped was to output the xdebug trace in human readable format with mem deltas enabled (an INI setting, xdebug.show_mem_deltas or something I think?). Then run sort (if you are on *nix) on the output:

sort -bgrk 3 -o sorted.txt mytracefile.xt

That sorts on the third col, the mem deltas. You can also sort on the second column, in which case you can find the line at which your app uses the most memory in total.

Of course, this can't detect when an object's memory usage only creeps up in small increments but ends up using a lot of memory overall. I have a fairly dumb method that attempts to do this using a combination of object iteration and serialization. It probably doesn't equate exactly to memory usage, but hopefully gives an idea of where to start looking. Bear in mind it will use up memory itself, and also has not been extensively tested, so buyer beware:

function analyzeMem($obj, $deep=false)
{
    if (!is_scalar($obj))
    {
     $usage = array('Total'=>strlen(serialize($obj)));
     while (list($prop, $propVal) = each($obj)) 
     {
      if ($deep && (is_object($propVal) || is_array($propVal)))
      {
       $usage['Children'][$prop] = analyzeMem($propVal);
      }
      else
      {
       $usage['Children'][$prop] = strlen(serialize($propVal));
      }
     }
     return $usage;
    }
    else
    {
     return strlen(serialize($obj));
    }
}

print_r(analyzeMem(get_defined_vars()));

Also, just got suggested this method by a colleague (cheers Dennis ;-) This hides the steps that are below 2 levels of indentation, you can quite easily see the points where the overall memory usage jumps up, and can narrow things down by increasing the indentation:

egrep '[0-9]+ (  ){1,2}-> ' mytracefile.xt
EvilPuppetMaster
+1  A: 

On http://www.xdebug.org/updates.php for Xdebug 2.0.4 they write in section "removed functions": "...Removed support for Memory profiling as that didn't work properly...". Hence xdebug wont be an option

+1  A: 

Mhmm. I need to do some serious memory analyzing, too. I wonder if this link helps: http://derickrethans.nl/xdebug-and-tracing-memory-usage.html Have to test this tomorrow.

chris