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. I have overlooked the small details. Of course, it is inevitable that my level is limited and there are some omissions. I warmly welcome everyone.
phper to discuss together.
First of all, let’s see the 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.php The output result is very clear:
hy0kl% php -f gc.php
I am test.
OK, 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 take a look :
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 the same as Example 3.
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 at:
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, I was surprised when I ran this code for the first time, but it made me wonder. Now that you have a deeper understanding of PHP GC, the following example of working with it will naturally be 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 viewer, then you can close this window. Welcome to come back when you have time!
Let’s analyze GC and references in detail.
1. In all examples, a variable is created. In layman’s terms, this process is to open up a space in the memory and store a string I in it. am test.. There is a symbol table inside PHP to record the reference count of each block of memory. At this time, the reference count of this block of memory will be increased by 1, and a label (variable) named $a will point to this block of memory. , which makes it easy to operate the memory according to the tag name.
2. Perform & operation on variable $a. My understanding is to find the memory pointed by $a, establish the same reference point for $b, and store the memory block of the string I am test. in the symbol table. The reference count is increased by 1. In other words, when our script executes 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 point, not Pointers, PHP has no concept of pointers! At the same time, some people have proposed file soft links 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 variable $a and
$b is a soft link established for a real file, 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. And through a certain A soft link operates the file similarly.
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?
Look at the following code and its result:
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 disconnected the pointer, 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 operation of assigning null is quite violent. It will directly set the reference count in the symbol number of the memory pointed to by the variable to 0. Then this memory will naturally be reclaimed by the engine. As for how It is unknown when it will be used again. It may be used to store other information immediately, and may never be used again. But in any case, all the original variables pointing to that memory will no longer be able to operate the reclaimed memory. Any attempt to Any variable calling 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.
In summary, it fully explains why we are looking at the source code of open source products Sometimes, we often see that some relatively large temporary variables, or reused information that is no longer called after use, 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 Naturally it became an empty chain
The above introduces the reference passing of PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.