Php garbage collection
Example #1 generate a new zval container
$ A = "new string ";
?>
In the preceding example, the new variable a is generated in the current scope. A variable container of the string type and the new string value is generated. In the additional two bytes, "is_ref" is setFALSEBecause no custom reference is generated. "Refcount" is set to 1 because only one variable uses this variable container. Note that when the value of "refcount" is 1, the value of "is_ref" is alwaysFALSE. If you have installed» Xdebug, you can call the functionXdebug_debug_zval ()Displays the values of "refcount" and "is_ref.
Example #2 display zval information
Xdebug_debug_zval ('A ');
?>
The above routine will output:
a: (refcount=1, is_ref=0)='new string'
Assigning a variable to another variable will increase the number of references (refcount ).
Example #3 add a zval reference count
$ A = "new string ";
$ B = $;
Xdebug_debug_zval ('A ');
?>
The above routine will output:
a: (refcount=2, is_ref=0)='new string'
At this time, the number of references is 2, because the same variable container is associated with variable a and variable B. php does not copy the generated variable container when it is not necessary. The variable container is destroyed when "refcount" is changed to 0. when any variable associated with a variable container leaves its scope (for example, function execution ends), or the function unset () is called for the variable, "refcount" is reduced by 1, the following example shows how:
Example #4 reduce reference count
$ A = "new string ";
$ C = $ B = $;
Xdebug_debug_zval ('A ');
Unset ($ B, $ c );
Xdebug_debug_zval ('A ');
?>
The above routine will output:
a: (refcount=3, is_ref=0)='new string'a: (refcount=1, is_ref=0)='new string'
If we execute unset ($ a);, the variable container containing the type and value will be deleted from the memory.