Tracing PHP Memory Usage using Xdebug and Mamp on Mac
I recently wanted to see which parts of my app were using the most memory in a PHP script. I’ve been using Xdebug and webgrind on MAMP on my local machine and loving it. Derick Rethans wrote a post a few years ago about how to use Xdebug to profile memory usage and it was fairly easy to get it working.
-
Setup Xdebug on your Mac on MAMP.
-
Set Xdebug to output the needed function traces by adding the following to the php5.3.6.ini file:
xdebug.show_mem_delta = 1
xdebug.trace_format = 1
xdebug.trace_enable_trigger = 1
xdebug.auto_trace = 1
xdebug.trace_output_dir = "/tmp"
How to edit php .ini file in MAMP:
In context screenshot:
- Restart MAMP
-
Download Derick’s parsing script to parse out memory usage from .xt trace files. I downloaded the script to my home directory to keep it simple.
-
Run PHP script that you want to measure memory usage from and confirm that .xt trace file is output to the /tmp directory. My output file was named
/tmp/trace.2043925204.xt
- Run Derick’s memory parsing script against output .xt file
~/memory.php /tmp/trace.2043925204.xt memory-own 20
Example output:
parsing...
Done.
Showing the 20 most costly calls sorted by 'memory-own'.
Inclusive Own
function #calls time memory time memory
-----------------------------------------------------------------
{main} 1 0.0345 2303008 0.0085 1894608
EpiRoute->addRoute 107 0.0058 97344 0.0034 92208
EpiApi->addRoute 61 0.0014 49304 0.0014 49304
EpiRoute->get 64 0.0050 65280 0.0017 6608
include_once 8 0.0003 6376 0.0002 6120
Epi::getSetting 108 0.0024 5184 0.0024 5184
dbConnection 2 0.0007 4760 0.0001 4568
EpiApi->get 33 0.0062 63112 0.0014 4224
EpiRoute->post 43 0.0037 42736 0.0012 4064
EpiApi->post 28 0.0058 54088 0.0013 3584
getApi 61 0.0011 3344 0.0011 3344
mysqli->prepare 2 0.0003 2640 0.0003 2640
require 4 0.0003 2312 0.0002 2312
session_start 1 0.0001 2216 0.0001 2216
include 5 0.0006 3192 0.0003 880
fsockopen 2 0.0015 880 0.0015 880
func_get_args 2 0.0000 840 0.0000 840
EpiTemplate->display 1 0.0008 4288 0.0002 776
EpiRoute::getInstance 108 0.0022 528 0.0022 528
EpiRoute->getRoute 1 0.0002 728 0.0001 480