Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools to visually analyze memory usage of a PHP app

Tags:

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.

like image 740
Oleg Barshay Avatar asked Nov 01 '08 18:11

Oleg Barshay


People also ask

How much memory does PHP use?

A typical appropriate memory limit for PHP running Drupal is 128MB per process; for sites with a lot of contributed modules or with high-memory pages, 256MB or even 512MB may be more appropriate. It's often the case that the admin section of a site, or a particular page, uses much more memory than other pages.

How much memory does each PHP process consume?

When I set the PHP memory limit to e.g. 128 MB for each of these daemons, the processes will only get killed once they reach 128 MB according to PHP's own measurements. However, according to ps , the processes will be using around 200 MB each by that time.


1 Answers

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

like image 81
Francesco Casula Avatar answered Sep 20 '22 09:09

Francesco Casula