Home  >  Article  >  Backend Development  >  Understanding of PHP garbage collection mechanism

Understanding of PHP garbage collection mechanism

巴扎黑
巴扎黑Original
2016-11-11 10:01:50904browse

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. .


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn