How does PHP's garbage collection work, and how does it handle circular references?

Last updated 15 June 2026

  • Reference counting is the main strategy: each zval tracks a refcount and 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 via zend.enable_gc. It matters mostly in long-running CLI workers since requests free everything at shutdown.

  • https://www.geeksforgeeks.org/python/python-cheat-sheet/
All PHP interview questions