Understanding the GC Function in PHP
PHP uses reference counting to determine when a variable becomes eligible for garbage collection. This can be problematic, as if a variable is part of a cyclic reference (e.g. $A points to $B and back to $A) then even if the number of symbols that point to that variable drops to zero, it will still be in a cyclic reference and won’t get collected. This is referred to as a memory leak and can be incredibly difficult to detect, particularly when it comes to third party libraries that don’t follow the same reference counting rules.
The solution to this problem is the garbage collector which can be triggered by the gc_enabled function. It is a little complicated and there are a lot of things that can affect its performance, but the GC can help to reduce memory consumption in long running scripts. However, understanding the GC is a little bit of a mystery and you may not be sure how to optimize its usage for your specific application.
In PHP 8.3 the gc_status() function was updated to include additional fields that can be used to gain insight into the GC. However, these fields are not available with user-land functions and require recompiling to gain access to them.
These extra fields can be extremely useful when debugging memory issues in your web applications. They can help you identify a variety of problems such as the amount of time spent freeing values during cycle collections and the length of time it takes to collect cycles, among others.