Home  >  Article  >  Backend Development  >  Understanding PHP garbage collection mechanism_PHP tutorial

Understanding PHP garbage collection mechanism_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:18726browse

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
 代码如下 复制代码

error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;

echo $b ."n";
?>

不用说 % php -f gc.php 输出结果非常明了:
hy0kl% php -f gc.php
I am test.

error_reporting(E_ALL);
$a = 'I am test.' ;

$b = & $a;
代码如下 复制代码

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?

echo $b ."n";
?>

 代码如下 复制代码

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.

Needless to say, the output result of % php -f gc.php is very clear :

hy0kl% php -f gc.php

I am test.


Okay, next one:

Example 2:
 代码如下 复制代码

error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;

unset($b);

echo $a ."n";
echo $b ."n";
?>

The code is as follows Copy code
error_reporting(E_ALL); <🎜>$a = 'I am test.'; <🎜>$b = & $a;<🎜><🎜>$ b = 'I will change?';                                                                         Still obvious :< 🎜>hy0kl% php -f gc.php <🎜>I will change?<🎜>I will change?<🎜>
<🎜>Please take a look:<🎜>Example 3:<🎜>
The code is as follows Copy code
<🎜>error_reporting(E_ALL); <🎜>$a = 'I am test.'; <🎜>$b = & $a; <🎜> <🎜>unset($a);<🎜><🎜>echo $a ."n"; <🎜>echo $b ."n";<🎜>?>Do you have to think about it? ?hy0kl% php -f gc.php Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8I am test.
Are you a little confused?Look again:Example 4: tr>
Code As follows Copy code
< ?php <🎜>error_reporting(E_ALL); <🎜>$a = 'I am test.'; <🎜>$b = & $a;<🎜><🎜>unset($b);                                                                       >echo $a ."n"; <🎜>echo $b ."n";<🎜>?>

In fact, if you understand Example 3, this is similar to it.
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:
Example 5:

The code is as follows
 代码如下 复制代码

error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;

$a = null;

echo '$a = '. $a ."n";
echo

Copy code



error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;

$a = null;

echo '$a = '. $a ."n" ;
代码如下 复制代码

error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;

$b = null;

echo '$a = '. $a ."n";
echo '$b = '. $b ."n";
?>

echo

'$b = '. $b ."n";
?>What is the first feeling of fierceness? ?

hy0kl% php -f gc.php

$a = Yes, this is the output result. PHPers who already have a deep understanding of PHP GC will not think there is anything wrong with it. Strange, to be honest, I was surprised when I ran this code for the first time, 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:
The code is as follows Copy code
$b = & $a;$b = null;

echo '$a = '. $a ."n"; echo '$b = '. $b ."n";
?> This is a PHP tutorial. You can also go to Baidu to learn more about PHP garbage collection mechanism articles. I won’t introduce them one by one here. http://www.bkjia.com/PHPjc/444592.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444592.htmlTechArticleThe PHP garbage collection mechanism is only available after php5. Let me introduce to you about PHP garbage collection. I have some understanding of the mechanism, I hope it will be helpful to all the students. Before php 5.3...
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