The PHP garbage collection mechanism is something that only existed after php5. Let me introduce to you some understanding of the PHP garbage collection mechanism. I hope it will be helpful to all students.
The garbage collection mechanism used before PHP 5.3 is a simple "reference counting", that is, each memory object is allocated a counter. When the memory object is referenced by a variable, the counter is 1; when the variable reference is removed, the counter -1 ; When the counter = 0, it indicates that the memory object is not used, the memory object is destroyed, and garbage collection is completed.
The problem with "reference counting" is that when two or more objects refer to each other to form a ring, the counter of the memory object will not be reduced to 0; at this time, this group of memory objects is no longer useful, but it cannot Recycling, thus causing memory leaks;
Starting from php5.3, a new garbage collection mechanism has been used. Based on reference counting, a complex algorithm has been implemented to detect the existence of reference rings in memory objects to avoid memory leaks. .