How does PHP's garbage collection work, and how does it handle circular references?
Last updated
Reference counting is the main strategy: each
zvaltracks arefcountand is freed immediately at 0.Cycles keep nonzero counts, so a cycle collector buffers candidate roots and, when full, simulates removing internal references to find and free unreachable groups.
It can be triggered with
gc_collect_cycles()or controlled viazend.enable_gc. It matters mostly in long-running CLI workers since requests free everything at shutdown.