PHP memory profiling
Asked Answered
A

4

105

What's a good way to profile a PHP page's memory usage? For example, to see how much memory my data is using, and/or which function calls are allocating the most memory.

  • xdebug doesn't seem to provide memory information in its profiling feature.

  • xdebug does provide it in its tracing feature. This is pretty close to what I want, except the sheer amount of data is overwhelming, since it shows memory deltas for every single function call. If it were possible to hide calls below a certain depth, maybe with some GUI tool, that would solve my problem.

Is there anything else?

Agnes answered 19/5, 2009 at 0:34 Comment(4)
Check out Rasmus Lerdorf's talk called "Simple is Hard" (talks.php.net/show/froscon08 for slides, youtube.com/watch?v=RWRYX5eJbG0 for video). He goes over a lot of useful tools such as "inclued" (pecl.php.net/package/inclued), xdebug, and KCacheGrind.Hinkel
I found a patch to xdebug, which provides memory information in the profiles. So far it's working very well.Agnes
As you say, xdebug provides info in function traces. Luckily, they also provide a script to interpret that. derickrethans.nl/xdebug-and-tracing-memory-usage.html It seems to be working for me so far...Oaken
possible duplicate of Tools to visually analyze memory usage of a PHP appHite
K
15

Xdebug reimplemented memory tracing in 2.6 (2018-01-29) which can be used in Qcachegrind or similar tool. Just make sure to select the memory option :)

From the docs:

Since Xdebug 2.6, the profiler also collects information about how much memory is being used, and which functions aGnd methods increased memory usage.

I'm not familiar with the format of the file, but it's Qcachegrind has worked great for me in tracing a couple memory issues.

qcachegrind sample

Kirshbaum answered 19/2, 2018 at 19:18 Comment(4)
And they even used my ticket. :)Agnes
Xdebug dropped support for PHP 5 in the 2.6 release.Medusa
If it helps anyone... when using php-fpm, pid might not change between different request. With default profiler_output_name, this leads xdebug to overwrite previous data. Change this value using something else from xdebug.org/docs/all_settings#trace_output_namePiccadilly
im trying to use qcachegrind now and don't understand what I'm looking at. I don't see memory numbers anywhere. How much memory does each variable take up? I want something that can trace the execution and show the memory at each point, and what variables are taking up that memory . Where can I get more information on this?Makeyevka
H
78

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

Hite answered 21/5, 2014 at 9:55 Comment(3)
How did you make it to work? I tried to put memprof_enable into my PHP code and I get PHP Fatal error: Uncaught Error: Call to undefined function memprof_enable(). I did gperftools make install from current source code of them.Sublapsarianism
Run a php -i on the cli or a phpinfo() to see if you're getting the extension loaded properly. If you don't have it there it's probably worth taking a look at your *.ini files.Hite
Note: the latest version supports only php7. If you use php5, install with sudo pecl install memprof-1.0.0.Embassy
P
19

Well, this may not be exactly what you're looking for, but PHP does have a couple of functions built-in that will output memory usage. If you just wanted to see how much memory a function call is using, you could use memory_get_peak_usage() before and after a call, and take the difference.

You use the same technique around your data using the very similar memory_get_usage().

Pretty unsophisticated approach, but it's a quick way to check out a piece of code. I agree that xdebug mem deltas can be too verbose to be useful sometimes, so I often just use it to narrow down to a section of code, then dump out specific memory usage for small pieces manually.

Plante answered 19/5, 2009 at 0:46 Comment(0)
K
15

Xdebug reimplemented memory tracing in 2.6 (2018-01-29) which can be used in Qcachegrind or similar tool. Just make sure to select the memory option :)

From the docs:

Since Xdebug 2.6, the profiler also collects information about how much memory is being used, and which functions aGnd methods increased memory usage.

I'm not familiar with the format of the file, but it's Qcachegrind has worked great for me in tracing a couple memory issues.

qcachegrind sample

Kirshbaum answered 19/2, 2018 at 19:18 Comment(4)
And they even used my ticket. :)Agnes
Xdebug dropped support for PHP 5 in the 2.6 release.Medusa
If it helps anyone... when using php-fpm, pid might not change between different request. With default profiler_output_name, this leads xdebug to overwrite previous data. Change this value using something else from xdebug.org/docs/all_settings#trace_output_namePiccadilly
im trying to use qcachegrind now and don't understand what I'm looking at. I don't see memory numbers anywhere. How much memory does each variable take up? I want something that can trace the execution and show the memory at each point, and what variables are taking up that memory . Where can I get more information on this?Makeyevka
G
0

http://geek.michaelgrace.org/2012/04/tracing-php-memory-usage-using-xdebug-and-mamp-on-mac/

I'm on a Mac so if you're on Windows you'll have to test this, but this works for me.

I modified my tracefile-analyzer.php file and added the path to the PHP binary at the top so that you could call it in terminal as a normal unix script.

#!/Applications/MAMP/bin/php5.3/bin/php
<?php
if ( $argc <= 1 || $argc > 4 )
{

Don't forget to chmod this file to 755.

You could easily create a ruby watchr script to automatically call the script each time it creates a memory profile file (*.xt). That way you could keep testing and seeing your improvements without having to execute the command over and over.

Ginelle answered 18/9, 2012 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.