Profiling memory usage

Last updated on
18 September 2024

This documentation needs review. See "Help improve this page" in the sidebar.

Original article is here, follow that for most of the process: http://derickrethans.nl/xdebug_and_tracing_memory_usage.php

Alternatively you can get the tracefile-analyser.php file from xdebug/xdebug github repo. 

This below settings in only applicable if you are using xdebug v2.*

zend_extension="/usr/lib/php5/20060613/xdebug.so"
xdebug.profiler_enable_trigger = on
xdebug.profiler_output_dir = /home/catch/www/cachegrind
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_host = localhost
xdebug.profiler_output_name = cachegrind.out.%R
xdebug.profiler_append = TRUE
xdebug.auto_trace = 1
xdebug.trace_format = 1

If you are using xdebug v3.* or above then use the below settings.

xdebug.client_host=localhost # host.docker.internal if you are using ddev.
xdebug.client_port=9003
xdebug.start_with_request=yes
xdebug.mode=trace
xdebug.output_dir=/var/www/html/xdebug-profiler
xdebug.trace_output_name=trace.%R.out
xdebug.trace_format=1

Please note the following settings has been removed or renamed in xdebu V3.*.

Xdebug V2.* Xdebug V3.*
xdebug.profiler_enable_trigger xdebug.start_with_request
xdebug.profiler_output_dir xdebug.output_dir
xdebug.remote_enable xdebug.mode # With differernt values
xdebug.remote_port xdebug.client_port
xdebug.remote_host xdebug.client_host
xdebug.auto_trace xdebug.start_with_request

When you run the script sorting by 'memory own' - i.e. how much memory each function consumes, you'll get a table like this:

Showing the 30 most costly calls sorted by 'memory-own'.

                                               Inclusive        Own
function                               #calls  time     memory  time     memory
-------------------------------------------------------------------------------
drupal_load                                37  0.0140   799248  0.0055   535792
_registry_check_code                        7  0.0032   475792  0.0010   321200
_drupal_bootstrap_full                      1  0.0414  1721632  0.0016   303904
has_krumo                                   1  0.0038   301152  0.0033   300848
drupal_bootstrap                            3  0.0717  3154272  0.0012   295224
_drupal_bootstrap_database                  1  0.0006   258424  0.0005   248528
module_load_include                         7  0.0024   196928  0.0012   194248
require_once                               31  0.0055   437816  0.0026   114032
unserialize                                 8  0.0004    86696  0.0004    86696
Database::openConnection                    1  0.0012    80344  0.0003    73944
system_list                                 3  0.0092    64296  0.0016    51344
ob_start                                    1  0.0000    41016  0.0000    41016
drupal_static                              63  0.0041    31560  0.0028    31560
require                                     4  0.0007   149464  0.0002    19848
module_implements                           8  0.0032    86808  0.0006    13824
PDOStatement->fetchObject                   6  0.0002    10760  0.0002    10760
PDOStatement->execute                      11  0.0056    10592  0.0056    10592
include_once                               34  0.0066   212480  0.0024    10344
module_list                                 4  0.0097    74128  0.0002     9560
_drupal_bootstrap_variables                 1  0.0058   131056  0.0002     8552
_drupal_bootstrap_page_cache                1  0.0088   402848  0.0003     8496
variable_get                               39  0.0009     8320  0.0009     8320
drupal_get_filename                        85  0.0039     7408  0.0029     7408
PDO->prepare                                7  0.0008     6784  0.0003     5920
MergeQuery_mysql->__toString                1  0.0002     7552  0.0001     5744
drupal_environment_initialize               1  0.0005     7312  0.0002     5416
DatabaseConnection->prefixTables           11  0.0011    10304  0.0006     5248
define                                    174  0.0035     5200  0.0035     5200
strtr                                      11  0.0003     5056  0.0003     5056
drupal_settings_initialize                  1  0.0018    12600  0.0004     4784

I've also attached a patch to xdebug/api which makes it work on PHP5.2 - only works with memory-own sorting, but that's good enough for our purposes.

Help improve this page

Page status: Needs review

You can: