In PHP, the Garbage Collector (GC) is responsible for automatically manages memory, freeing up resources that are no longer in use by your scripts, preventing memory leaks. PHP primarily uses reference counting, but it also has a cycle collector to deal with more complex scenarios like circular references.
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.
1. Reference Counting
-
Every variable in PHP has a reference count.
-
When you assign a variable or pass it to a function, the reference count increases.
-
When the reference count drops to zero, PHP frees the memory.
Example:
No comments:
Post a Comment