This functionality is now merged into Xdebug, please use it from there

garbage collection statistics for PHP

DEPRECATED/UNMAINTAINED The functionality of this extension is now part of Xdebug version 2.6.0. This extension is therefore going out of maintenance.

Collect statistics about every run of the PHP garbage collector and its efficency.

The PHP garbage collector runs when to many objects or arrays are in use and attempts to detect the ones with cycles that are not in use anymore and clean them up. The PHP engine keeps statistics about this internally, but doesn't expose them to userland.

We extracted this functionality from Tideways, our production profiler for your PHP applications. You can automatically collect information about garbage collector performance and efficiency in Tideways from production traffic.

Blog Post: How to optimize the PHP Garbage collector usage to improve memory and performance


  • Linux
  • PHP 7+


This repository is a PHP extension and must be installed as an extension into PHP. For your global PHP installation in your systemt the installation after cloning this repository:

sudo make install

If you have multiple PHP versions the setup boils down to:

./configure --with-php-config=/path/to/php-config
sudo make install


For CLI scripts, you can print out a report immediately from the extension without having to change the code:

$ php -dgc_stats.enable=1 -dgc_stats.show_report=1 my_script.php
## Garbage Collection Statistics for PHP by Tideways ##
Use to get realtime information about garbage collection in production.

Found 7 garbage collection runs in current script.

Collected | Efficency% | Duration | Memory Before | Memory After | Reduction% | Function
        0 |     0.00 % |  0.01 ms |        365856 |       366352 |    -0.14 % | gc_collect_cycles
    10000 |   100.00 % |  3.19 ms |       4651352 |       491848 |    89.43 % | foo
     9000 |    90.00 % |  2.27 ms |       4694712 |       951208 |    79.74 % | Test::foo

Enable the gc run collection by setting the ini variable:

ini_set('gc_stats.enable', 1);

To access the statistics call $retun = gc_stats():

$stats = gc_stats();

array(1) {
  array(6) {
    string(17) "gc_collect_cycles"
    array(1) {
      array(3) {
        string(%d) "/tmp/test.php"
        string(17) "gc_collect_cycles"

Each run entry contains several data points:

  • collected is the number of items that the garbage collector cleaned up.
  • duration is the time in microseconds it took to perform the cleanup.
  • memory_before is the value of memory_get_usage() before the cleanup was started.
  • memory_after is the value of memory_get_usage() after the cleanup.
  • function is the name of the PHP function inside which the garbage collection is triggered. If this is gc_collect_cycles() it means the trigger was done explicitly, with all other function names triggering happend automatically when the GC is enabled.
  • stack is the return value of debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)

Please refer to for more similar questions