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.

1

8 Answers 8

13

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

Sign up to request clarification or add additional context in comments.

7 Comments

sudo pecl install memprof doesn't get pass make stage. Gives error:Makefile:194: recipe for target 'memprof.lo' failed
It is important to note that php-memory-profiler does not support php7.
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.
@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.
Worth mentioning to use "Using Google gperftools (recommended!)" you need to install php-memory-profiler first, those php functions don't come from gperftools.
|
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

Comments

5

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 Comment

Looks like it's been re-added in 2017 since your comment. [2017-12-02] — Xdebug 2.6.0alpha1 Added features Implemented bug #474: Added "memory" output to profiling files, to find out where memory is allocated.
2

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.

Comments

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

Comments

0

A graphical tool for xdebug output is KCacheGrind.

1 Comment

I am aware of KCacheGrind and WinCacheGrind. Neither tool allows analysis of memory usage as far as I can tell.
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.

1 Comment

Unfortunately webgrind does not report memory usage -- just execution speed.
0

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

2 Comments

Does it handle memory usage profiling or just performance profiling? From their website I only saw performance profiling.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.