PHP Garbage Collection: Memory growth in long-running processes with circular references
29K reputation · 20 Jun 2022, 17:35 UTC
In long-running PHP processes, such as worker scripts or daemons, memory consumption may increase over time despite the use of reference counting. While PHP employs a cycle collector to handle circular references, the timing and efficiency of this reclamation can impact stability when scripts operate near the memory_limit threshold.
There is a trade-off between the automatic triggering of the garbage collector and the CPU overhead introduced by manual calls to gc_collect_cycles(). In environments with large, complex object graphs, circular references may not be reclaimed quickly enough to prevent memory exhaustion.
What are the specific triggers that cause the internal cycle collector to run automatically? How does the memory overhead of the cycle buffer affect the overall heap usage before a collection is triggered?