How does the PHP Garbage Collector work, specifically regarding circular references?

Last updated 15 June 2026

  • PHP frees most values immediately via reference counting: each zval has a refcount and 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, and gc_enable()/gc_disable() toggle the cycle collector (refcounting always runs).

  • https://www.geeksforgeeks.org/python/destructors-in-python/
All PHP interview questions