Home > Backend Development > PHP Tutorial > Garbage Collection A brief explanation of PHP garbage collection mechanism

Garbage Collection A brief explanation of PHP garbage collection mechanism

WBOY
Release: 2016-07-29 08:43:21
Original
1200 people have browsed it

Although I am also a learner of PHP, I really didn’t know much about the garbage collection process inside PHP before. We just used unset, null, mysql_close, __destruct and other functions in our code to release objects to prevent memory overflow, so I found the following instructions under GG on the Internet and made a record: "PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Every time Each object contains a reference counter, and each reference is connected to the object. When the reference leaves the living space or is set to NULL, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will. This object is no longer needed and the memory space it occupies is released.”
As we all know, the PHP engine itself is written in C. When mentioning C, we must mention GC (garbage collection). Through the PHP manual, we know that the PHP engine will automatically perform GC actions. So we can’t help but ask, what exactly does it do? How is it recycled? Is the & reference operation a pointer? When unset() is used to unset() a variable, is it really recycled? These seem to be issues mentioned in the manual, but if you analyze them carefully, you will find that they are far from simple and general. . Maybe someone will jump out and say: You can know it by looking at the PHP source code. Yes, after you read through the PHP source code, this problem will definitely go away. However, this article will only analyze these seemingly ordinary but commonly used problems from PHP itself. Of course, there are small details that have been overlooked. Of course, there are inevitably some omissions due to limited skill levels. We warmly welcome all phpers to discuss together.
First of all, let’s see an example, the simplest execution process:
Example 1: gc.php
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
echo $b ." ";
?>
Needless to say% php -f gc. The php output result is very clear:
hy0kl% php -f gc.php
I am test.
Okay, next:
Example 2:
error_reporting(E_ALL);
$a = 'I am test. ';
$b = & $a;
$b = 'I will change?';
echo $a ." ";
echo $b ." ";
?>
The execution result is still obvious:
hy0kl % php -f gc.php
I will change?
I will change?
Please see:
Example 3:
error_reporting(E_ALL);
$a = 'I am test.';
$ b = & $a;
unset($a);
echo $a ." ";
echo $b ." ";
?>
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 8
I am test.
Are you a little confused?
Look again:
Example 4:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($b);
echo $a ." ";
echo $b ." ";
?>
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 here:
Example 5:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$a = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
What is the first feeling of fierceness?
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 for the first time I was surprised when I saw this piece of code, 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:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
OK, if the result of the above example does not have any details for the reader, then you can close this window and welcome to come back when you have time!
Let’s analyze GC and references in detail.
1. In all examples, a variable is created. To put it simply, this process opens up a space in the memory and stores a string I am test in it. There is a symbol table inside PHP to record references to each block of memory. Count, then the reference count of this memory will be increased by 1, and a label (variable) named $a will be used to point to this memory, so that the memory can be operated according to the label name.
2. Perform & on the variable $a Operation, my understanding is to find the memory pointed to by $a, establish the same reference point for $b, and add 1 to the reference count of the memory block storing the string I am test. in the symbol table. In other words, our When the script is executed to this line, the memory storing the string I am test. is referenced twice. What should be emphasized here is that the & operation establishes a reference pointer, not a pointer. PHP has no concept of pointers! At the same time, Some people have suggested that file soft links are similar to UNIX. It can be understood to a certain extent: the memory storing the character I am test. is a real file of ours, and the variables $a and $b are established for the real file. Soft links, but they point to the same real file. So, we see that when $b is assigned a value in Example 2, the value of $a also changes. It is similar to operating a file through a soft link.
3. In Examples 3 and 4, the unset() operation was performed. According to the actual execution results, it can be seen that: unset() only disconnects the reference of the variable to the memory it originally pointed to, making the variable itself undefined. If a null reference is passed, a Notice is issued when the call is made, and the reference count of that piece of memory in the symbol table is decremented by 1, and it does not affect other variables pointing to this piece of memory. In other words, only when a piece of memory has a reference count in the symbol table When it is 0, the PHP engine will reclaim this memory.
PHP Manual
4.0.0 unset() became an expression. (In PHP 3, unset() would always return 1).
What does this mean?
See Look at the following code and its results:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
unset($a) ;
unset($a);
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 10
$a =
$b = I am test.
The first unset() operation has been disconnected point, so subsequent operations will not affect the reference count of any memory in the symbol table.
4. Through Example 5 & 6, it can be clearly concluded that the assignment null operation is quite violent, it will directly change the variable The reference count of the memory pointed to in the symbol is set to 0, so this memory is naturally reclaimed by the engine. It is unknown when it will be used again. It may be used to store other information immediately, or it may never be used again. Passed. But no matter what, all the original variables pointing to that memory will no longer be able to operate the reclaimed memory, and any variable trying to call it will return null.
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
if (null === $a)
{
echo '$a is null.';
} else
{
echo 'The type of $a is unknown.';
}
?>
hy0kl% php - f gc.php
$a =
$b =
$a is null.
To sum up, it fully explains why when we look at the source code of open source products, we often see some relatively large temporary variables, or after use Reused information that is no longer called will be concentrated or displayed as null. It is equivalent to directly killing the real file in UNIX, and all soft links pointing to it will naturally become empty links.

The above introduces the garbage collection. A brief explanation of the PHP garbage collection mechanism, including the content of garbage collection. I hope it will be helpful to friends who are interested in PHP tutorials.

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