Home > Article > Backend Development > Understanding of PHP garbage collection mechanism
[Introduction] The PHP garbage collection mechanism is only available after php5. Now I will 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 and 3 was a simple reference count, that is, each memory object is allocated a count. The PHP garbage collection mechanism is something that only came after php5. Let me introduce to you about PHP garbage. I hope it will be helpful to you to understand the recycling mechanism.
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. .
You can refer to the following article for this algorithm, 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
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; echo $b ."n"; ?>
Needless to say% php -f gc.php The output result is very clear:
hy0kl% php -f gc.php I am test.
Okay, next one:
Example 2:<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = 'I will change?'; echo $a ."n"; echo $b ."n"; ?> 执行结果依然很明显: hy0kl% php -f gc.php I will change? I will change?
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($a); echo $a ."n"; echo $b ."n"; ?>
hy0kl% php -f gc.php Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8 I am test.
Are you a little confused?
Look again:
Example 4:<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($b); echo $a ."n"; echo $b ."n"; ?>
hy0kl% php -f gc.php I am test. Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
Look again:
Example 5:<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $a = null;echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; ?>
hy0kl% php -f gc.php $a = $b =
Yes, this is the output result. PHPers who have a deep understanding of PHP GC will not find it strange. To be honest, when I ran this code for the first time It was unexpected, but it gave me a deeper understanding of PHP GC. Then the following example of working with it is naturally easier to understand.
Example 6:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = null; echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; ?>
The above is the understanding of PHP garbage collection mechanism, more For related content, please pay attention to the PHP Chinese website (www.php.cn)!