PHP garbage collection mechanism

Source: Internet
Author: User

the garbage collection mechanism of PHP is a reference counting method, which is not the same as that of JavaScript, and is a bit different from normal reference counting.     JavaScript markup cleanup is better understood when variables enter the environment, labeled "Go to the environment," and leave the environment as "out of the environment." Variables that leave the environment are generally cleared, and if the variables in the environment have references to variables that leave the environment, they cannot be cleared. The reference count for PHP is this.         each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first one is "Is_ref",The second extra byte is "RefCount". These two sentences are directly copied from PHP manual. or directly on the code good.
<?php$c = array( ‘one‘ );$c[] =& $c;$d =& $c;xdebug_debug_zval   (    ' C '     echo $d[0] ."<br />";unset($c);xdebug_debug_zval( ‘c‘ );xdebug_debug_zval( ‘d‘ );
here is the result of the execution.
in the code above, there are two zval variable containers, one of which is the address of an array, and there are three variables C and D that point to the address, and the value of the second half of the address itself, and the other is the value "one", which holds the string type, and only a pointer to that value. That is the value of the first half of the previous address. Because of this, when D refers to C, the reference count of the second container does not increase, and after the relationship between the variable C and the first variable container is cut off, that is , a unset($c), or can access the one value via D[0] because the memory space for this value is not freed. However, after the execution of the program, the memory space of this one value will be freed, because the normal situation of leaving the execution Environment (scope) or call Unset,refcount will be reduced by one, 1-1=0, is recycled. But the first zval is tragic, because it always has a reference to itself, even if the script is done, RefCount always 1. This means that PHP's reference count does not solve the problem of recursive references, but since php5.3, the problem has been solved, but not understood.As for the general reference count, this is probably the case.
instances A and B, respectively, have a variable to refer to each other, when the reference count is 2, after the program is finished, it becomes 1, the memory can not be released. If this is PHP, two instances are just two zval variable containers, counting the two zval variables inside two instances, 4 separate zval. The first two reference counts are 2, respectively, the next two are 1, after the execution of the program, 1-1=0, cleared two instances of the internal variables, two variables on the value is not, the value is a reference to the instance A and B, cleared, 2 becomes 1, instance A and B then remove the reference, the last two pieces of memory is released.
I don't know if this is the right idea, but the general reference count appears as a circular reference becausewhen an object is recycled, the reference count of other objects referenced by the object should be reduced, and the above object AB is not recycled, waiting for the other person to get into a dead loop. If only a refers to B, and B does not refer to a, then a refcount of 1,b is 2, a can be recycled from the beginning, and a reference to B is also released, 2-1=1, and finally B can be recycled.
so the reference count of PHP and the general is not quite the same?

PHP garbage collection mechanism

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.