PHP garbage collection mechanism-PHP Tutorial

Source: Internet
Author: User
A brief description of the PHP garbage collection mechanism. Although I am also a PHP learner, I didn't know much about the garbage collection process in PHP before, but I used unset, null, mysql_close in our code, __destruct and other functions although they are also PHP learners, they didn't really know much about the garbage collection process in PHP before, just used unset, null, mysql_close in our code, __destruct and other functions to release objects to prevent memory overflow. therefore, go to GG and find the following instructions to record: "PHP can automatically perform memory management, clear unnecessary objects. PHP uses reference counting, a simple garbage collection mechanism. Each object contains a reference counter. each reference is connected to an object, and the counter is incremented by 1. When the reference leaves the living space or is set to NULL, the counter minus 1. When the reference counter of an object is zero, PHP knows that you no longer need to use this object, releasing the memory space occupied by it ."

As we all know, the PHP engine itself is written in C. GC (garbage collection) cannot be mentioned in C ). we learned through the PHP manual that the PHP engine will automatically perform GC operations. so we can't help wondering how it is recycled, and whether the reference operation is a pointer. When unset () is a variable, is it actually recycled? If you carefully analyze the issues that seem to be mentioned in the manual, you will find that they are far from that simple and general. someone may jump out and say: I don't know the PHP source code. yes. after you have read the PHP source code, this question will be answered. However, this article will only analyze the small details that seem to have been ignored by PHP itself. of course, it is inevitable that the level is limited and there are some omissions. We warmly welcome phper to discuss them together.

First, we can see the example, which is the simplest execution process:
Example 1: gc. php
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Echo $ B ."";
?>

Needless to say, the output of % php-f gc. php is very clear:
Hy0kl % php-f gc. php
I am test.

Okay. next:
Example 2:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = 'I will change? ';

Echo $ ."";
Echo $ B ."";
?>
The execution results are still obvious:
Hy0kl % php-f gc. php
I will change?
I will change?

Please refer:
Example 3:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ );

Echo $ ."";
Echo $ B ."";
?>
Do you have to try it out?
Hy0kl % php-f gc. php
Notice: Undefined variable: a in/usr/local/www/apache22/data/test/gc. php on line 8
I am test.
A little confused?

Let's look at it again:
Example 4:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ B );

Echo $ ."";
Echo $ B ."";
?>
In fact, if Example 3 understands this, it 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

You can also see:
Example 5:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ A = null;

Echo '$ a ='. $ ."";
Echo '$ B ='. $ B ."";
?>
What is the first feeling of fierce competition?
Hy0kl % php-f gc. php
$ A =
$ B =
Yes, this is the output result. phper, which has a deep understanding of php gc, does not feel any strange. To be honest, it was very unexpected when I ran this code for the first time, but it gave me a deeper understanding of php gc. the examples below are similar to those below.

Example 6:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = null;

Echo '$ a ='. $ ."";
Echo '$ B ='. $ B ."";
?>

OK. if the result of the above example does not have any details for the viewer, you can close this window!

Next we will analyze GC and references in detail.
1. in all the examples, a variable is created. In other words, a space is opened up in the memory and a string I am test is stored in it .. PHP has a symbol table to record the reference count of each block of memory. in this case, the reference count of this block of memory is added to 1 and a tag named $ a (variable) is used) point to this memory to facilitate memory operation according to the tag name.

2. perform the & operation on the variable $ a. In my understanding, find the memory that $ a points to, create the same reference for $ B, and store the string I am test. memory block in the symbol table reference count plus 1. in other words, when the script runs to this line, it stores the string I am test. the memory is referenced twice. what should be emphasized here is that & the operation creates a reference point, not a pointer. PHP does not have the pointer concept! At the same time, it was suggested that file soft links are similar to those of UNIX. it can be understood to a certain extent: store the character I am test. and $ a and $ B are soft links for real files, but they point to the same real file. so, we can see that while assigning values to $ B in Example 2, the value of $ a also changes. similar to operating a file through a soft link.

3. in examples 3 and 4, the unset () operation is performed. according to the actual execution results, it can be seen that unset () only disconnects the reference of the variable to the memory that it originally pointed to, making the variable itself a null reference without definition, the Notice is issued during the call, and the reference count in the symbol table is reduced by 1, which does not affect other variables pointing to this memory. in other words, the PHP engine will reclaim the memory only when the reference count in the symbol table is 0.
PHP Manual
4.0.0 unset () became an expression. (In PHP 3, unset () wowould always return 1 ).
What does this mean?
Let's take a look at the following code and its results:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ );
Unset ($ );
Unset ($ );

Echo '$ 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 will not affect any memory reference records of the symbol table.

4. you can use Example 5 & 6 to make it clear that the null assignment operation is quite fierce, and it will directly set the reference count in the symbol number pointed to by the variable to 0, the memory is naturally recycled by the engine. it is unknown when it will be reused. it may be immediately used to store other information and may never be used again. however, in any case, all the previously directed memory variables will no longer be able to operate on the recycled memory, and any variable trying to call it will return null.

Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = null;

Echo '$ a ='. $ ."";
Echo '$ B ='. $ B ."";

If (null ===$)
{
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 often see some large temporary variables when looking at the source code of open-source products, or the reuse information that is no longer called after use will be set or the displayed value is null. it is equivalent to killing real files directly in UNIX, and all soft links pointing to it naturally become empty chains.

...

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.