How does the PHP Garbage Collector work, specifically regarding circular references?
Last updated
PHP frees most values immediately via reference counting: each
zvalhas arefcountand is freed when it hits 0.Circular references (A points to B, B points to A) never reach 0, so a cycle collector buffers possible roots and, when the buffer fills (default 10000), frees groups referenced only internally.
gc_collect_cycles()forces a collection, andgc_enable()/gc_disable()toggle the cycle collector (refcounting always runs).