Home > Article > Backend Development > What is PHP garbage collection mechanism? Detailed explanation of PHP garbage collection mechanism
PHP’s garbage collection mechanism can be simply summarized as reference counting and copy-on-write COW mechanism.
This article mainly shares with you the knowledge of mastering the PHP garbage collection mechanism, hoping to help everyone. [Recommended tutorial: php tutorial]
Basic knowledge of reference counting
The official website’s answer is as follows: Each php variable has a zval variable in a variable container called "zval" In addition to the type and value of the variable, the container also includes two bytes of additional information is_ref and refcount. is_ref is a bool value used to identify whether this variable belongs to a reference set. Through this byte, the PHP engine can distinguish ordinary variables from reference variables. Refcount is used to represent the number of variables pointing to this zval variable container. Reference counting in PHP5. In PHP5, the memory of zval is separated from the heap. allocated (with a few exceptions), PHP needs to know which zvals are in use and which ones need to be freed. So this requires the use of reference counting: the value of refcount__gc in zval is used to save the number of times zval itself is referenced. For example, in the b = 12 statement, 12 is referenced by two variables, so its reference count is 2. If the reference count becomes 0, it means that the variable is no longer used and the memory can be released.
As follows:
<?php //php zval变量容器 $a = 1; $b = 1; $c = &$a; $d = $b; $e = range(0, 3); xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); xdebug_debug_zval('d'); xdebug_debug_zval('e'); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The results are as follows
a: (refcount=2, is_ref=1), int 1b: (refcount=2, is_ref=0), int 1c : (refcount=2, is_ref=1), int 1d: (refcount=2, is_ref=0), int 1e: (refcount=1, is_ref=0), array (size=4) 0 => (refcount= 1, is_ref=0), int 0 1 => (refcount=1, is_ref=0), int 1 2 => (refcount=1, is_ref=0), int 2 3 => (refcount=1, is_ref=0), int 3
<?php //php zval变量容器 $a = 1; $b = 1; $c = &$a; $d = $b; $e = range(0, 3); xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); xdebug_debug_zval('d'); xdebug_debug_zval('e'); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The results are as follows a: (refcount=2, is_ref=1) int 1b: (refcount=0, is_ref=0) int 1c: (refcount=2, is_ref=1 ) int 1d: (refcount=0, is_ref=0) int 1e: (refcount=1, is_ref=0) array (size=4) 0 => (refcount=0, is_ref=0) int 0 1 => (refcount=0, is_ref=0) int 1 2 => (refcount=0, is_ref=0) int 2 3 => (refcount=0, is_ref=0) int 3
Ordinary variables are not Then remember your own number. For complex types such as arrays, remember your own number. What is garbage? Only under criterion 3, the GC will collect the zval, and then use a new algorithm to determine whether the zval is garbage. So how to judge whether such a variable is real garbage? To put it simply, the refcount is reduced by 1 for each element in the zval. After the operation is completed, if the refcount of the zval=0, then the zval is garbage. If the refcount of a zval increases, then the zval is still in use. , not garbage. If the refcount of a zval is reduced to 0, then the zval can be released. If the refcount of a zval is reduced to greater than 0, then the zval cannot be released, and the zval may become garbage.
The above is the detailed content of What is PHP garbage collection mechanism? Detailed explanation of PHP garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!