Home >Backend Development >PHP Tutorial >Explain the PHP garbage collection mechanism in detail through examples
PHP garbage collection mechanism:
1. PHP can automatically manage memory and clear unnecessary objects, mainly using reference counting
2 . Ref_count and is_ref are defined in the zval structure. ref_count is a reference count, which identifies how many variables this zval is referenced. When it is 0, it will be destroyed. is_ref identifies whether the & address character is used to force a reference
3. In order to solve the problem of circular reference memory leaks, a synchronous cycle recycling algorithm is used.
For example, when an array or object cyclically refers to itself and unsets the array, if refcount-1 is still greater than 0, it will be regarded as suspected garbage, traversed, and simulated deletion of refcount- 1 If it is 0, delete it. If it is not 0, restore the stubborn garbage generation process:
<?php $a = "new string"; ?>
a: (refcount_gc=1, is_ref_gc=0)='new string'
When $a is assigned to another variable, the refcount_gc of the zval corresponding to $a will be increased by 1
<?php $a = "new string"; $b = $a; ?>
At this time, the internal storage information corresponding to the $a and $b variables is, $a and $b point to a string "new string" at the same time, and its refcount becomes 2a,b: (refcount_gc=2,is_ref= 0)='new string'
When unset is used to delete the $b variable, the refcount_gc of "new string" will be reduced by 1 and become 1
For ordinary variables, all this It's normal, but in composite type variables (arrays and objects), something more interesting will happen:
<?php $a = array('meaning' => 'life', 'number' => 42); ?>
$aThe internal storage information is:
a: (refcount=1, is_ref=0)=array ( 'meaning' => (refcount=1, is_ref=0)='life', 'number' => (refcount=1, is_ref=0)=42 )
The array variable itself ($a) Inside the engine is actually a hash table. There are two zval items meaning and number in this table, so in fact a total of 3 zvals are generated in that line of code. These 3 zvals all follow the reference and counting principles of variables. Use The diagram shows:
# Next, add an element to $a and assign the value of an existing element to the new element:
<?php $a = array('meaning' => 'life', 'number' => 42); $a['name'] = $a['meaning']; ?>
Then the internal storage of $a is, the ref_count of "life" becomes 2, and the ref_count of 42 is 1:
a: (refcount=1, is_ref=0)=array ( 'meaning' => (refcount=2, is_ref=0)='life', 'number' => (refcount=1, is_ref=0)=42, 'name' => (refcount=2, is_ref=0)='life' )
If you assign the reference of the array to an element in the array, interesting things will happen:
<?php $a = array('one'); $a[] = &$a; ?>
In this way, the $a array has two elements, one with an index of 0 and a value of the character one, and the other with an index of 1, which is a reference to $a itself. 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)=… )
array The ref_count of this zval is 2, which is a circular reference. At this time, $a is unset, then $a will be deleted from the symbol table, and the refcount_gc of the zval pointed to by $a is reduced by 1.
Then the problem arises, $a is no longer in the symbol table, the user This variable can no longer be accessed, but the refcount_gc of the zval pointed to by $a becomes 1 instead of 0, so it cannot be recycled, resulting in a memory leak. The job of the new GC is to clean up such garbage.
In order to solve the problem of circular reference memory leaks, a synchronous cycle recycling algorithm is used. If the ref_count is reduced by 1 and is still greater than 0, it will be regarded as suspected garbage.
For example, when an array or object cyclically refers to itself and unsets the array, if refcount-1 is still greater than 0, it will be traversed and simulated to delete refcount-1 once. If it is 0, it will be deleted. If it is not 0, restore it.
If you want to know more related content, please visit the PHP Chinese website: PHP Video Tutorial
The above is the detailed content of Explain the PHP garbage collection mechanism in detail through examples. For more information, please follow other related articles on the PHP Chinese website!