Garbage collection PHP garbage collection mechanism simple description

Source: Internet
Author: User
Tags php source code
Although I am also a PHP learner, but did not really understand the PHP internal garbage collection process, just in our code with unset,null,mysql_close,__destruct and so on some functions to release the object to prevent memory overflow, so online GG, Here are some instructions to make a note "PHP can automate memory management to clear objects that are no longer needed." PHP uses the simple garbage collection (garbage collection) mechanism of the reference count (reference counting). Each object contains a reference counter, each reference connected to the object, and the counter is incremented by 1. When reference leaves the living space or is set to NULL, the counter is reduced by 1. When a reference counter for an object is zero, PHP knows that you will no longer need to use this object to free up the memory space it occupies. ”
As we all know, the PHP engine itself is written in C, the mention of C can not mention is the GC (garbage collection). We learned from the PHP manual that the PHP engine will automatically perform GC actions. Then we can't help but wonder how it's recycled, & the reference operation is not a pointer, unset ( A variable when it is not really recycled it? These seemingly manual questions, if analyzed carefully, will find that it is far less simple and general. Maybe someone will jump out and say: Look at the PHP source code does not know. Yes, when you read through the PHP source, this problem must be easy, but this article is only from PHP In itself to analyze these seemingly ordinary but neglected small details, of course, the inevitable level of limitations, some omissions, warmly welcome the broad masses of phper to discuss together.
First we see the example, the simplest implementation process:
Example 1:gc.php
Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;
echo $b. " ";
?>
Needless to say, the php-f gc.php output 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'll change? ';
echo $a. " ";
echo $b. " ";
?>
The results of the implementation are still obvious:
hy0kl% php-f gc.php
I'll change?
I'll change?
June please 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?
June Look again:
Example 4:
Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;
Unset ($b);
echo $a. " ";
echo $b. " ";
?>
In fact, if Example 3 understand, this is similar.
hy0kl% php-f gc.php
I am Test.
notice:undefined variable:b in/usr/local/www/apache22/data/test/gc.php on line 9
June and look:
Example 5:
Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;
$a = null;
echo ' $a = '. $a. " ";
echo ' $b = '. $b. " ";
?>
What is the first sensation of the fierce?
hy0kl% php-f gc.php
$a =
$b =
Yes, this is the output, and the PHP GC has an in-depth understanding of the Phper does not feel strange, to tell the truth, when I first run this code is very unexpected, but let me have a better understanding of the PHP GC. So the following examples of working with it are naturally understandable.
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 to crossing, then you can close this window, welcome to come again!
Let's take a detailed analysis of GC and reference.
1. In all cases, a variable is created, which is a common point: it opens up a space in memory where a string I am test is stored. . There is a symbol table inside PHP to record each block memory reference count, then the reference count of this memory will be added 1, and a tag named $a (variable) point to the memory, convenient for the tag name to manipulate memory.
2. & operation of variable $a, my understanding is to find the memory that the $a points to, and to establish the same reference point for the $b, and will store the string I am test. The memory block in the symbol table refers to the Count plus 1. In other words, when our script executes to this line, the string I am test is stored. That memory was quoted two times. Here to emphasize that the & operation is to establish a reference point, not a pointer, PHP does not have a pointer to the concept! At the same time, it was suggested that a soft link to a file similar to UNIX can be understood to some extent: store the character I am test. That memory is one of our real files, and the variables $a and $b are soft links built for real files, but they point to the same real file. So, as we can see, the value of the $a is changed as well as the $b assigned in Example 2. Similar to manipulating files through a soft chain.
3. In Example 3 and 4, the unset () operation is performed. According to the actual execution result, it can be seen that: unset () just disconnects the variable from its original reference to the memory, so that the variable itself is not defined as a null reference, where the call issued a Notice, and that the block exists The reference count minus 1 in the symbol table does not affect other variables that point to the memory. In other words, the PHP engine will recycle this memory only if the reference count in the symbol table in the block is 0 o'clock.
PHP Manual
4.0.0 unset () became an expression. (In PHP 3, unset () would always return 1).
What does that mean?
Take a look at the following code with 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 been disconnected, so subsequent operations do not affect the reference count of any memory in the symbol table.
4. By Example 5 & 6 It is clear that the assignment of a null operation is quite fierce, and it will directly set the reference count of the memory pointed to by the variable to 0, which is naturally recycled by the engine, and when it is not known again, it may be used as a store of other information immediately. It may never have been used. But anyway, all that point to that memory variable will no longer be able to manipulate the reclaimed memory, and any variables that attempt to invoke 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, fully explain why we are looking at the source code, we often see some of the larger temporary variables, or the use of the reuse information that is no longer called will be set or display the assignment is null. It is equivalent to the UNIX directly to the real file out, all the soft links to it naturally become empty chain.

The above describes the garbage collection PHP garbage collection mechanism simple description, including the content of garbage collection, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.