1 Reference Count
Reference counting is the addition of a field record in value refcount
to the number of current value, a variable copy, a function that does not directly copy a copy of value data, but will refcount++
, when the variable is destroyed, refcount--
until refcount
Minus 0 indicates that no variable has been referenced by this value, and it can be destroyed.
The storage structure is as follows:
1typedefstruct_zend_refcounted_h {2uint32_t RefCount;/*Reference Counter 32-bit*/3 Union {4 struct {5 Zend_endian_lohi_3 (6 Zend_uchar Type,7Zend_uchar flags,/*used for Strings & objects*/8uint16_t gc_info)/*keeps GC root number (or 0) and color*/9 } V;Ten uint32_t type_info; One } u; A} Zend_refcounted_h;
The information for the reference count is in the GC for the specific value structure, for example in a string variable:
1 struct _zend_string zend_string; 2 struct _zend_string {3 zend_refcounted_h GC; 4 Zend_ulong H; /* */5 size_t len; 6 Char val[1]; 7 };
2 copy on write
Reference count, multiple variables may point to the same value, and then through the RefCount statistical reference number, at this time if one of the variables try to change the contents of value will be re-copy a copy of value modification, while disconnecting the old point, the mechanism of the copy is a very wide application in the computer system, It only takes place when necessary (write) to make a hard copy, which can improve the efficiency.
Not all types can be copied, such as objects, resources, real-time only String, array two support, the same as the reference count, but also by zval.u1.type_flag
identifying whether value can be replicated
3 Variable Recycling
There are two main types of recycling of PHP variables: active destruction, automatic destruction. the main action is to destroy the unset. Automatic Destruction is the automatic management mechanism of PHP, in return, minus the local variable refcount, even if there is no explicit return,php will automatically add this operation, and the other is to write when the copy will break the original value point, The refcount of the old value after disconnection is also checked.
4 Garbage Collection
The collection of PHP variables is implemented according to RefCount, and when unset, return, the reference count of the variable is lost, and if RefCount is reduced to 0, the value is released directly, which is the simple GC process of the variable.
However, in the actual process of the GC can not reclaim the memory leak caused by the bug, first look at the next example:
1 $a = [1]; 2 $a [] = &$a; 3 unset ($a);
As can be seen, unset($a)
since there are child elements in the array point $a
, so refcount > 0
, can not be recycled through a simple GC mechanism, this variable is garbage, garbage collector to deal with this situation, the current garbage will only appear in the array, object two types, So the only special treatment for these two cases: when a variable is destroyed, if it is found that the loss of RefCount is still greater than 0, and the type is Is_array, is_object this value into the GC may be garbage doubly linked list, When the chain is expressed to a certain number, the startup checker checks all variables once and destroys the release if it is determined to be garbage.
Assignment and destruction of PHP variables