PHP garbage collection mechanism

Source: Internet
Author: User
Tags php website

PHP5.2 version of the garbage collection mechanism mainly for the zval structure in the Refcount=0 release variables, but composite types of arrays or objects, ring reference or reference themselves, are caused by memory leaks, PHP website Introduction 5.3 GC mechanism, by putting zval into root Buffer, perform a simulated delete (refcount-1), when Refcount==0 is considered to be garbage while recycling, here are 4 steps:

For example:


  
   

The $ A array has two elements, an index of 0, a character one, an index of 1, and a reference to $ A itself, which is stored internally as follows:
A: (refcount=2, is_ref=1) =array (
0 = (refcount=1, is_ref=0) = ' One ',
1 = (refcount=2, is_ref=1) = ...
)
"..." means that 1 points to a itself, which is a circular reference:

According to the online expression said: First put in root buffer, refcount-1, then a $ A refcount=1, and then unset, Refcount=0, then think $ A as garbage
Problem:
1. I now assume that a $ A array has 2 keys referencing itself, such as:


  
   

A: (refcount=3, is_ref=1) =array (
0 = (refcount=1, is_ref=0) = ' One ',
1 = (refcount=3, is_ref=1) = ...
2 = (refcount=3, is_ref=1) = ...
)
Then the array after the unset is definitely a garbage, so for example I put in buffer, refcount-1, then refcount=2, and then unset-1,refcount=1, so it seems that although this array delete will cause garbage, But according to the new GC mechanism, it is only when refcount=0 is considered rubbish, is it not a contradiction? Is it not as if the >=2 key quoted itself as garbage?

Reply content:

PHP5.2 version of the garbage collection mechanism mainly for the zval structure in the Refcount=0 release variables, but composite types of arrays or objects, ring reference or reference themselves, are caused by memory leaks, PHP website Introduction 5.3 GC mechanism, by putting zval into root Buffer, perform a simulated delete (refcount-1), when Refcount==0 is considered to be garbage while recycling, here are 4 steps:

For example:


  
   

The $ A array has two elements, an index of 0, a character one, an index of 1, and a reference to $ A itself, which is stored internally as follows:
A: (refcount=2, is_ref=1) =array (
0 = (refcount=1, is_ref=0) = ' One ',
1 = (refcount=2, is_ref=1) = ...
)
"..." means that 1 points to a itself, which is a circular reference:

According to the online expression said: First put in root buffer, refcount-1, then a $ A refcount=1, and then unset, Refcount=0, then think $ A as garbage
Problem:
1. I now assume that a $ A array has 2 keys referencing itself, such as:


  
   

A: (refcount=3, is_ref=1) =array (
0 = (refcount=1, is_ref=0) = ' One ',
1 = (refcount=3, is_ref=1) = ...
2 = (refcount=3, is_ref=1) = ...
)
Then the array after the unset is definitely a garbage, so for example I put in buffer, refcount-1, then refcount=2, and then unset-1,refcount=1, so it seems that although this array delete will cause garbage, But according to the new GC mechanism, it is only when refcount=0 is considered rubbish, is it not a contradiction? Is it not as if the >=2 key quoted itself as garbage?

Did you find the answer? I try to answer the next

According to the landlord said, look at the following content:

$a = 123;$b = $a;   //a: (refcount=2, is_ref=0)=123   b: (refcount=2, is_ref=0)=123unset($b);    //a: (refcount=1, is_ref=0)=123

At this point a point of the zval will also enter the garbage cycle, and then to RefCount minus one, is not the symbol a pointed to the zval also to be deleted? Obviously not.

Here's what I'm talking about: The GC does not subtract one of the possible garbage root elements, but it does so only if the element below the root element is reduced by one operation.
In this case, the root node Zval count will not be reduced by one operation, when circular reference, the circular reference point to the zval count minus one, so no matter how many circular references can be reduced to 0, this is not possible to say.

Look closely at the picture, don't you see the problem? Clearly produced a self-reference, but why refcount still 1? It should be the reason that the variable was released into buffer refcount-- . That's what you don't understand.

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