Understanding of PHP garbage collection mechanism

巴扎黑
Release: 2023-03-03 12:52:01
Original
5489 people have browsed it

[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 = &#39;I am test.&#39;; 
$b = & $a;
echo $b ."n"; 
?>
Copy after login

Needless to say% php -f gc.php The output result is very clear:

hy0kl% php -f gc.php 
I am test.
Copy after login

Okay, next one:

Example 2:

<?php 
error_reporting(E_ALL); 
$a = &#39;I am test.&#39;; 
$b = & $a;
$b = &#39;I will change?&#39;;                                                         
echo $a ."n"; 
echo $b ."n"; 
?>
执行结果依然很明显:
hy0kl% php -f gc.php 
I will change?
I will change?
Copy after login

Please take a look:

Example 3:

<?php 
error_reporting(E_ALL); 
$a = &#39;I am test.&#39;; 
$b = & $a; 
unset($a);
echo $a ."n"; 
echo $b ."n";
?>
Copy after login

Do you have to think about it? What?

hy0kl% php -f gc.php 
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8
I am test.
Copy after login

Are you a little confused?

Look again:

Example 4:

<?php 
error_reporting(E_ALL); 
$a = &#39;I am test.&#39;; 
$b = & $a;
unset($b);                                                                     
echo $a ."n"; 
echo $b ."n";
?>
Copy after login

In fact, if you understand Example 3, this one has the same purpose.

hy0kl% php -f gc.php 
I am test.
Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
Copy after login

Look again:

Example 5:

<?php error_reporting(E_ALL); 
$a = &#39;I am test.&#39;; 
$b = & $a;
$a = null;echo &#39;$a = &#39;. $a ."n"; 
echo &#39;$b = &#39;. $b ."n"; 
?>
Copy after login

What is the first feeling of violence?

hy0kl% php -f gc.php 
$a = 
$b =
Copy after login

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 = &#39;I am test.&#39;; 
$b = & $a;
$b = null;
echo &#39;$a = &#39;. $a ."n"; 
echo &#39;$b = &#39;. $b ."n"; 
?>
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!