php-src icon indicating copy to clipboard operation
php-src copied to clipboard

GC optimization of closure use itself

Open Yurunsoft opened this issue 4 years ago • 4 comments

Description

code:

<?php

function test()
{
    $a = static function () use (&$a) {
    };
}

test();

var_dump(gc_collect_cycles());

output:

int(1)

expected output:

int(0)

Yurunsoft avatar Feb 15 '22 03:02 Yurunsoft

Why do you expect 0? test() creates a cycle and gc_collect_cycles() finds and removes it.

damianwadley avatar Feb 15 '22 03:02 damianwadley

Under resident memory, if there is no manual gc, the memory usage will gradually increase.

Yurunsoft avatar Feb 15 '22 07:02 Yurunsoft

PHP will automatically GC cycles when they reach 10000. https://3v4l.org/r2F3l

https://www.php.net/manual/en/features.gc.collecting-cycles.php

When the garbage collector is turned on, the cycle-finding algorithm as described above is executed whenever the root buffer runs full. The root buffer has a fixed size of 10,000 possible roots (although you can alter this by changing the GC_ROOT_BUFFER_MAX_ENTRIES constant in Zend/zend_gc.c in the PHP source code, and re-compiling PHP). When the garbage collector is turned off, the cycle-finding algorithm will never run. However, possible roots will always be recorded in the root buffer, no matter whether the garbage collection mechanism has been activated with this configuration setting.

Please describe the bug you are trying to report.

damianwadley avatar Feb 15 '22 07:02 damianwadley

Thanks!

Yurunsoft avatar Feb 15 '22 07:02 Yurunsoft