The Garbage Collector (GC) is the internal memory management system in PHP, but there are some subtleties to understand.
The GC automates memory management, which removes the hassle of handling memory with manual tasks (which would be tedious).
This allows developers to focus on their business logic without worrying excessively about 'Out of Memory' errors.
Of course, it's not magic.
Freeing objects that are no longer needed prevents memory leaks.
The GC uses a counting mechanism to determine the elements to drop. If no references point to a particular object (i.e., $counter = 0), then this object is eligible for cleanup.
It works pretty well, but some references can be problematic:
class A { public $b; } class B { public $a; } $a = new A(); $b = new B(); $a->b = $b; $b->a = $a; unset($a); unset($b);
In this case of poor design, PHP will not free the memory even if we unset $a and $b, as they reference each other, leading PHP to believe they are still in use.
Fortunately, there is another mechanism called the Cycle Collector for that:
gc_collect_cycles();
Roughly speaking, the collector traverses all references and applies an algorithm to mark objects in use, which reveals objects to collect (the unmarked ones).
However, PHP does not trigger automatic cycle collection until the thresholds of 10,000 objects with potential cyclic references is reached.
Again, it's not magic, so you must invoke gc_collect_cycles() only in few cases.
Bad design can lead to overcomplex relationships between objects, leading to more references and more frequent garbage collection.
Each reference-counted object requires additional storage for its reference count.
Source: Wikipedia - Reference counting
The overhead associated with memory cleanup operations can impact the global performance significantly and ultimately increase the execution time in specific scenarios.
10 years ago, Composer got a huge performance boost just by using the gc_disable() function.
Source: Composer - disabling GC
Indeed, PHP 7 drastically improved the GC, so it is not what it was in 2014.
In addition, PHP 8 versions improved memory allocation strategies and added more useful statistics about GC operations for better monitoring (gc_status() in 8.3).
Most PHP applications are request-driven, and the memory is automatically cleared at the end of the request.
Again, it's pretty cool but not magic. What happens with asynchronous requests and long-lived objects/daemons?
You may experience memory leaks at some point.
At this point, you might not see how the PHP's GC differ from other languages.
Most of the time, other languages do not rely on reference counting to collect garbage or may use different implementations.
For example, many use the tracing algorithm that also marks unused objects but does not operate incrementally. It's a graph traversal.
Besides, some languages do not allow such direct control (e.g., on/off at runtime).
As usual, there are some advantages and inconvenients, so you may see some hybrid approaches.
You can leverage the built-in gc_* helpers.
For example:
These functions are helpful for debugging or fine-tuning garbage collection when necessary.
You can read this post for further insights:
PHP 7.4 introduced Weak References and PHP 8 introduced Weak Maps.
A Weak Map could be described as a collection of Weak References.
This data structure is a versatile key-value store that helps PHP keep track of items without creating clutter or consuming excessive space.
You may see it as a temporary storage that will be cleared right away when it's no longer needed, as there are no [strong] reference that could prevent the garbage collection:
class A { public $b; } class B { public $a; } $a = new A(); $b = new B(); $a->b = $b; $b->a = $a; unset($a); unset($b);
For most usages, you won't have to worry about memory management, as PHP already handles it.
However, because modern stacks utilize long-lived objects, you need to monitor your application for potential memory leaks.
If you get issues, you may have to optimize the code and/or interact with the GC directly.
The above is the detailed content of PHP: The Garbage Collector explained with simple words. For more information, please follow other related articles on the PHP Chinese website!