Reference Count: PHP variable exists in a variable container called "Zval". A Zval variable container, in addition to the type and value of the variable, contains additional information of two bytes: Is_ref and RefCount. Is_ref is a bool value that identifies whether the variable is a reference set, and the PHP engine distinguishes between ordinary and reference variables, and refcount is used to represent the number of variables that point to the Zval variable container, when refcount = 0 means that the variable can be purged or recycled.<?php$a = Array (' meaning ' = ' life ', ' number '); $a [' life '] = $a [' meaning '];xdebug_debug_zval (' a ');? >
The previous routine output is as follows:
A: (Refcount=1, Is_ref=0) =array ( ' meaning ' = (refcount=2, is_ref=0) = ' life ', ' number ' = = (Refcount=1, is _ref=0) =42, ' life ' (refcount=2, is_ref=0) = ' life ')
The output of the Xdebug is shown as two zval variable containers with a value of ' life ' , which is actually the same:
<?php$a = Array (' one '), $a [] =& $a; Xdebug_debug_zval (' a '); unset ($a); Xdebug_debug_zval (' a ');? >
The results of the above example are as follows:
(Refcount=1, Is_ref=1) =array ( 0 = (refcount=1, is_ref=0) = ' One ', 1 = (refcount=1, is_ref=1) = ...)
The previous example, although no longer has any symbols in a scope pointing to the variable container, the array element "1" still points to the array itself, so the container cannot be cleared. Because there is no additional symbol pointing to it, the user has no way to clear the structure, resulting in a memory leak. PHP will erase this data structure at the end of the script execution, but it will consume a lot of memory before PHP clears.