The garbage collection mechanism of PHP5.2 is mainly used to release variables when refcount0 is in the zval structure. However, if arrays or objects of the composite type are referenced or referenced cyclically, memory leakage occurs, the PHP official website introduces the GC mechanism of 5.3. zval is put into rootbuffer for simulated deletion (...
The garbage collection mechanism of PHP5.2 is mainly used to release variables when refcount = 0 in the zval structure. However, if arrays or objects of the composite type are referenced or referenced cyclically, memory leakage occurs, PHP official website introduction 5.3 GC mechanism, by zval into the root buffer, simulated deletion (refcount-1), when refcount = 0 is considered as garbage, while recycling, the following are four steps:
For example:
In this way, the $ a array has two elements. one index is 0, the value is one, and the other index is 1, which is a reference of $ a. The internal storage is as follows:
A: (refcount = 2, is_ref = 1) = array (
0 => (refcount = 1, is_ref = 0) = 'one ',
1 => (refcount = 2, is_ref = 1) =...
)
"..." Indicates that 1 points to a itself and is a circular reference:
According to the expression on the Internet: first put in the root buffer, refcount-1, then $ a refcount = 1, then unset, refcount = 0, it is considered $ a as garbage
Problem:
1. Now I assume that two keys in the $ a array reference myself, for example:
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 unset is certainly a garbage, then for example I put the buffer, refcount-1, then refcount = 2, then unset-1, refcount = 1, this seems to cause garbage when this array is deleted, but according to the new GC mechanism, only when refcount = 0 will it be considered as garbage. Isn't it a conflict? If the key> = 2 in my array references itself, it will not be treated as garbage?
Reply content:
The garbage collection mechanism of PHP5.2 is mainly used to release variables when refcount = 0 in the zval structure. However, if arrays or objects of the composite type are referenced or referenced cyclically, memory leakage occurs, PHP official website introduction 5.3 GC mechanism, by zval into the root buffer, simulated deletion (refcount-1), when refcount = 0 is considered as garbage, while recycling, the following are four steps:
For example:
In this way, the $ a array has two elements. one index is 0, the value is one, and the other index is 1, which is a reference of $ a. The internal storage is as follows:
A: (refcount = 2, is_ref = 1) = array (
0 => (refcount = 1, is_ref = 0) = 'one ',
1 => (refcount = 2, is_ref = 1) =...
)
"..." Indicates that 1 points to a itself and is a circular reference:
According to the expression on the Internet: first put in the root buffer, refcount-1, then $ a refcount = 1, then unset, refcount = 0, it is considered $ a as garbage
Problem:
1. Now I assume that two keys in the $ a array reference myself, for example:
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 unset is certainly a garbage, then for example I put the buffer, refcount-1, then refcount = 2, then unset-1, refcount = 1, this seems to cause garbage when this array is deleted, but according to the new GC mechanism, only when refcount = 0 will it be considered as garbage. Isn't it a conflict? If the key> = 2 in my array references itself, it will not be treated as garbage?
Have you found the answer? I will try to answer
As the landlord said, let's take a 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 time, zval pointed to by a will also enter the garbage cycle, and then subtract one from refcount. Is it necessary to delete zval pointed to by symbol? Apparently not
Here, based on what I know, I think: GC will not subtract one from the possible junk root elements, but will subtract one from the elements below the root element, this is reasonable.
Let's talk about this situation. The zval count of the root node will not be reduced by one. During Cyclic reference, the zval count pointed to by the cyclic reference will be reduced by one, in this way, the number of cyclic references can be reduced to 0.
Do you find any problems? A self-reference is generated, but why?refcount
Or 1? It should be that the variable is released to the buffer and thenrefcount--
. You don't understand this.