Master the detailed explanation of PHP garbage collection mechanism

  • 2021-11-29 23:19:54
  • OfStack

The garbage collection mechanism of php can be simply summarized as the replication COW mechanism when reference count is written,

This article mainly shares the knowledge of php garbage collection mechanism with you, hoping to help you.

Basic knowledge of reference counting

Official website's answer is as follows: Each php variable has one zval variable container in a variable container called "zval", which contains two bytes of additional information besides the type and value of the variable. is_ref and refcount is_ref are bool values, which are used to identify whether this variable belongs to the reference set (reference set). With this byte, the php engine can distinguish between ordinary variables and reference variables. refcount is used to indicate the number of variables pointing to this zval variable container. The reference count in PHP5 is in PHP5. The memory in zval is allocated separately from the heap (heap) (with a few exceptions). PHP needs to know which zval is in use and which needs to be released. So this requires a reference count: the value of refcount__gc in zval is used to hold the number of times zval itself has been referenced. For example, in the b = 12 statement, 12 is referenced by two variables, so its reference count is 2. If the reference count goes to 0, it means that the variable is no longer useful and memory can be freed.

As follows:


<?php 
//php zval Variable container 
$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

Each variable has its own number. zval in PHP7 has a new implementation of zval in PHP7. The most fundamental change is that the memory required by zval is no longer allocated separately from the heap and no longer stores reference counts by itself. Reference counts for complex data types, such as strings, arrays, and objects, are stored by themselves. This implementation has the following advantages: Simple data types do not need separate memory allocation, and do not need counting. There will be no counting twice. In objects, only the counts stored by the objects themselves are valid. Because the counts are now stored by the values themselves, they can be shared with data with non-zval structures. For example, the number of pointers required for indirect access between zval and hashtable key is reduced


<?php 
//php zval Variable container 
$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 no longer remember their own numbers, and complex types such as arrays remember their own numbers. Only under Rule 3, GC will collect zval, and then judge whether this zval is garbage through a new algorithm. So how to judge whether such a variable is real garbage? To put it simply, If refcount = 0, then zval is a garbage. If refcount of zval is increased, then zval is still in use and does not belong to garbage. If refcount of zval is reduced to 0, then zval can be released. If refcount of zval is reduced to 0, then zval cannot be released and zval may become a garbage. If refcount of zval is greater than 0


Related articles: