PHP garbage collection mechanism is briefly described

  • 2020-03-31 20:55:48
  • OfStack

Although PHP learner myself, but still didn't really understand before PHP internal recycling processes, just in our code using the unset, null, mysql_close, __destruct some function to release the object to prevent memory leaks, and so on, so the Internet GG, found the following some instructions, for the record "PHP can automatic memory management, remove objects no longer needed. PHP USES a simple garbage collection mechanism called reference counting. Each object contains a reference counter, which is incremented by 1 for each reference connected to the object. When the reference leaves the live space or is set to NULL, the counter is reduced by 1. When an object's reference counter is zero, PHP knows that you will no longer need to use the object, freeing up its memory.

As is known to all, the PHP engine itself is written in C, C it is mentioned GC (GC) through the PHP manual, we learned that the PHP engine will automatically GC actions. So we can not help but ask, exactly how it is recycling, & reference operation is a pointer, the unset () when a variable is it really be recycled? These seemingly manual is mentioned, if carefully analysis found that far less simple and general. Maybe someone will jump in to say the PHP source code is not to know. Yes, after waiting for you to read through the PHP source code will at this problem, but this will only from PHP itself to analyze these seemingly ordinary but neglected details, of course, the limited level of the hard to avoid, omissions, warmly welcome the masses of phper to discuss.

Let's start with an example of the simplest execution process:
Example 1: gc. PHP
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

Echo $b. "";
? >

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

Ok, next:
Example 2:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

$b = 'I will change? ';

Echo $a. "";
Echo $b. "";
? >
The results are still evident:
Hy0kl -f gc % PHP. PHP
I will change?
I will change?

You see:
Example 3:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

The unset ($a);

Echo $a. "";
Echo $b. "";
? >
Do you have to think about it?
Hy0kl -f gc % PHP. PHP
Notice: Undefined variable: a in/usr/local/WWW/apache22 / data/test/gc. PHP on line 8
I am the test.
A little confused?

You see:
Example 4:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

The unset ($b);

Echo $a. "";
Echo $b. "";
? >
In fact, if Example 3 is understood, this is the same thing.
Hy0kl -f gc % PHP. PHP
I am the test.
Notice: Undefined variable: b in/usr/local/WWW/apache22 / data/test/gc. PHP on line 9

Jun and see:
Example 5:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

$a = null;

Echo '$a = '. $a." ";
Echo '$b = '. $b." ";
? >
What is the first sensation?
Hy0kl -f gc % PHP. PHP
$a =
$b =
Yes, this is the output, and phper with a deep understanding of PHP GC won't be surprised, to be honest, when I first ran this code it was a surprise, but it gave me a much deeper understanding of PHP GC.

Example 6:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

$b = null;

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

OK, if the results of the above example are not any details to the viewer, you can close this window, welcome to come again!

Let's take a closer look at GC and references.
1. All the cases, create a variable, popular point: this process is opened up a piece of space, in memory to store a string in the inside I am test. The internal PHP has a symbol table, used to record every piece of memory reference counting, so at this time will be the memory of the reference count + 1, and use a named $a label (variables) to the memory, convenient to operate in accordance with the tag name of memory.

2. & operation of the variable $a, I understand it is to find the $a points to memory, and $b to establish a reference point to the same, I am and will be stored string test. The block of memory in the symbol table reference counting plus 1. In other words, our script execution to the line, hold the string I am test. The block of memory was cited twice. Here is to emphasize that & operation is to establish the reference point, instead of a pointer, PHP has no concept of pointer! And some say is similar to UNIX file soft links. Can to a certain extent, So understanding: hold characters I am test. The block of memory is one of our real file, and the variable $and $b is a soft link to real documents, but they point to the same file. So, we see that in Example 2 to $b assignment at the same time, the value of $a also follow changes. With a soft chain operation similar to the file.

3. In Example 3 and 4, the unset () operation. According to the actual results, it can be seen: the unset () just disconnect the variable to its original reference to memory, make the variable itself is not defined null reference, where the call issued Notice, and make that exist within reference counting minus 1 in the symbol table, does not affect the other pointing to the memory of the two variables. In other words, only when inside a piece of the reference count to zero in the symbol table, the PHP engine will recycle the memory.
The 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 and its results:
< ? PHP
Error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;

The unset ($a);
The unset ($a);
The unset ($a);

Echo '$a = '. $a." ";
Echo '$b = '. $b." ";
? >
Hy0kl -f gc % PHP. 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 broken the pointer, so subsequent operations do not affect any memory reference count in the symbol table.

4. Through Example 5 & 6 can clear: assign null operation is quite strong, it will directly to the variable points to the presence of reference counting in the symbol, 0, what about the engine recycling in the nature of memory, as to when to be used again is not clear, may soon be used to store any other information that may be never used. However, the original all point to the block of memory variables will be unable to operate by recycling of memory, any attempt to call its variables will return null.

< ? PHP
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.';
} the else
{
Echo 'The type of $a is unknown.';
}
? >
Hy0kl -f gc % PHP. PHP
$a =
$b =
$a is null.

To sum up, fully explains why we source in the open source products, often see some larger temporary variables, or the reuse of information will be used up no longer call focus or display value is null. It is equal to the UNIX will kill the real file directly, all links to its soft naturally become an empty.

Related articles: