GC optimization of closure use itself
Description
code:
<?php
function test()
{
$a = static function () use (&$a) {
};
}
test();
var_dump(gc_collect_cycles());
output:
int(1)
expected output:
int(0)
Why do you expect 0? test() creates a cycle and gc_collect_cycles() finds and removes it.
Under resident memory, if there is no manual gc, the memory usage will gradually increase.
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.
Thanks!