Understanding of the PHP garbage collection mechanism

Source: Internet
Author: User
Reading guidance 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, which means that each memory object is assigned a count

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

<?php 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:

<?php error_reporting (E_all); $a = ' I am Test. '; $b = & $a; $b = ' I'll change? ';                                                         echo $a. " n "; echo $b. " n ";?> execution results remain obvious: hy0kl% php-f gc.php I'll change? I'll change?

June please look:
Example 3:

<?php 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 8I am test.

Are you a little confused?

June Look again:
Example 4:

<?php 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:

<?php 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:

<?php                                                                         error_reporting (e_all); $a = ' I am test '; $b = & $a; $b = Null;echo ' $a = '. $a. " n "; echo ' $b = '. $b. " n ";?>

The above is the PHP garbage collection mechanism of understanding content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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