PHP garbage Collection Mechanism Understanding _php Tutorial

Source: Internet
Author: User
PHP garbage collection mechanism is php5 after this thing, I would like to introduce to you about the PHP garbage collection mechanism Some understanding, I hope you have some help.

The garbage collection mechanism used before PHP 5.3 is a simple "reference count", that is, each memory object is assigned a counter, when the memory object is referenced by a variable, counter 1; When the variable reference is removed, the counter-1; when the counter = 0 o'clock indicates that the memory object is not being used, the memory object is destroyed. Garbage collection is complete.

The problem with reference counting is that when two or more objects reference each other to form a ring, the counter of the memory object is not reduced to 0; At this time, this set of memory objects is useless, but cannot be recycled, resulting in memory leaks;

php5.3 started with a new garbage collection mechanism, based on reference counting, a complex algorithm was implemented to detect the existence of reference rings in memory objects to avoid memory leaks.

This algorithm can refer to the following article, which is the main reference of this small summary:) : Discussion on the evolution of garbage collection algorithm (garbage Collection) in PHP5


Look at the following example

Example 1:gc.php

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

echo $b. " n ";
?>

Needless to say, the php-f gc.php output is very clear:
hy0kl% php-f gc.php
I am Test.

OK, Next:
Example 2:

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

$b = ' I'll change? ';

echo $a. " n ";
echo $b. " n ";
?>
The results of the implementation are still obvious:
hy0kl% php-f gc.php
I'll change?
I'll change?

June please look:
Example 3:

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

unset ($a);

echo $a. " n ";
echo $b. " n ";
?>
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:

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

Unset ($b);

echo $a. " n ";
echo $b. " n ";
?>

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:

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

$a = null;

echo ' $a = '. $a. " n ";
Echo

' $b = '. $b. " n ";
?>
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:

The code is as follows Copy Code

Error_reporting (E_all);
$a = ' I am Test. ';
$b = & $a;

$b = null;

echo ' $a = '. $a. " n ";
echo ' $b = '. $b. " n ";
?>

This is a PHP tutorial, you can also Baidu to learn more about the PHP garbage collection mechanism article Oh, here is not introduced.


http://www.bkjia.com/PHPjc/444592.html www.bkjia.com true http://www.bkjia.com/PHPjc/444592.html techarticle PHP garbage collection mechanism is php5 after this thing, I would like to introduce to you about the PHP garbage collection mechanism Some understanding, I hope you have some help. PHP 5.3 before ...

  • 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.