Home > Article > Backend Development > Understanding PHP garbage collection mechanism_PHP tutorial
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.
There is a problem with "reference counting", that is, 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 cannot be recycled, resulting in memory leaks;
Starting from php5.3, a new garbage collection mechanism is used. Based on reference counting, a complex algorithm is implemented to detect references in memory objects. The existence of the ring to avoid memory leaks.
For this algorithm, you can refer to the following article, which is the main reference for this short summary:): A brief discussion on the evolution of the garbage collection algorithm (Garbage Collection) in PHP5
Look at the example below
Example 1: gc.php
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||
$a = 'I am test.' ; $b = & $a;
echo $b ."n";
hy0kl% php -f gc.php I am test.
In fact, if you understand Example 3, this is similar to it. Look:
?>What is the first feeling of fierceness? ? hy0kl% php -f gc.php 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 Previous article:How to retain the filled-in information after PHP fails to submit the form_PHP TutorialNext article:How to retain the filled-in information after PHP fails to submit the form_PHP Tutorial Related articlesSee more |