23

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.

Oleg Barshay
  • 3,798
  • 3
  • 21
  • 20

8 Answers8

12

As you probably know, Xdebug dropped the memory profiling support since the 2.* version. Please search for the "removed functions" string here: http://www.xdebug.org/updates.php

Removed functions

Removed support for Memory profiling as that didn't work properly.

So I've tried another tool and it worked well for me.

https://github.com/arnaud-lb/php-memory-profiler

This is what I've done on my Ubuntu server to enable it:

sudo apt-get install libjudy-dev libjudydebian1
sudo pecl install memprof
echo "extension=memprof.so" > /etc/php5/mods-available/memprof.ini
sudo php5enmod memprof
service apache2 restart

And then in my code:

<?php

memprof_enable();

// do your stuff

memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));

Finally open the callgrind.out file with KCachegrind

Using Google gperftools (recommended!)

First of all install the Google gperftools by downloading the latest package here: https://code.google.com/p/gperftools/

Then as always:

sudo apt-get update
sudo apt-get install libunwind-dev -y
./configure
make
make install

Now in your code:

memprof_enable();

// do your magic

memprof_dump_pprof(fopen("/tmp/profile.heap", "w"));

Then open your terminal and launch:

pprof --web /tmp/profile.heap

pprof will create a new window in your existing browser session with something like shown below:

PHP memory profiling with memprof and gperftools

Xhprof + Xhgui (the best in my opinion to profile both cpu and memory)

With Xhprof and Xhgui you can profile the cpu usage as well or just the memory usage if that's your issue at the moment. It's a very complete solutions, it gives you full control and the logs can be written both on mongo or in the filesystem.

For more details see my answer here.

Blackfire

Blackfire is a PHP profiler by SensioLabs, the Symfony2 guys https://blackfire.io/

If you use puphpet to set up your virtual machine you'll be happy to know it's supported ;-)

Community
  • 1
  • 1
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • `sudo pecl install memprof` doesn't get pass `make` stage. Gives error:`Makefile:194: recipe for target 'memprof.lo' failed` – Buddy Jun 23 '16 at 19:00
  • It is important to note that php-memory-profiler does not support php7. – Heath N Sep 15 '16 at 16:38
  • Should be accepted answer... Thanks for the info. Note that although kcachegrind/qcachegrind are designed for CPU analysis, when you analyze a cachegrind file spit out by php-memory-profiler, you'll find that the values and fields named correpsond to memory analysis. – Ezekiel Victor Oct 18 '16 at 18:08
  • @HeathN It does. There's the `master` branch in the repository that's PHP 7 only, and then there's the `php5` branch that supports PHP5 only. – starbeamrainbowlabs Apr 11 '17 at 10:07
  • Worth mentioning to use "Using Google gperftools (recommended!)" you need to install php-memory-profiler first, those php functions don't come from gperftools. – ManBehindTheCurtain Dec 28 '22 at 18:48
  • As far as I can tell, this comment is not longer accurate. Xdebug most definitely has memory profiling: https://xdebug.org/docs/profiler – nickc Apr 16 '23 at 21:51
  • @nickc we are a community, feel free to suggest an edit to the answer. – Francesco Casula Apr 17 '23 at 11:20
8

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
  • 8,072
  • 10
  • 34
  • 31
6

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

I personally used https://github.com/arnaud-lb/php-memory-profiler

on PHP 5.6 and Ubuntu 18, and Kcachegrind for visualizing.

Kcachegrind is okay, but not the best. I hope to find a better alternative even if it's on Mac or Windows.

Kharbat
  • 71
  • 7
1

With the version 2.6.0 on 2018-01-29 xdebug added support for profiling memory usage. Now you can generate callgrind files with time and memory information. On Mac you can visualize that information for example with Qcachegrind or Profiling Viewer (premium).

Profiling Viewer callgraph

user24525
  • 423
  • 3
  • 8
0

A graphical tool for xdebug output is KCacheGrind.

Marius Or.
  • 374
  • 4
  • 10
0

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.

rg88
  • 20,742
  • 18
  • 76
  • 110
0

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

user29772
  • 1,457
  • 7
  • 21
  • 25
  • Does it handle memory usage profiling or just performance profiling? From their website I only saw performance profiling. – Oleg Barshay Nov 02 '08 at 18:37
  • 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! – user29772 Nov 09 '08 at 21:47