Home >Backend Development >PHP Tutorial >Introduction to PHP garbage collection mechanism (code example)

Introduction to PHP garbage collection mechanism (code example)

不言
不言forward
2019-01-23 09:59:552304browse

This article brings you an introduction to the PHP garbage collection mechanism (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The garbage collection mechanism of PHP can be simply summarized as the reference counting copy-on-write COW mechanism.

This article mainly shares with you the knowledge of mastering the PHP garbage collection mechanism. I hope it can help everyone. .

Basic knowledge of reference counting

The official website’s answer is as follows Each PHP variable has a value called "zval" "In the variable containera zval variable container, in addition to containing the type and value of the variable, it 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 the 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 count in PHP5 In PHP5, zval memory is allocated separately from the heap (with a few exceptions), and PHP needs to know which zvals are in use and which ones need to be released. 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(&#39;a&#39;);
 xdebug_debug_zval(&#39;b&#39;); 
xdebug_debug_zval(&#39;c&#39;);
 xdebug_debug_zval(&#39;d&#39;);
 xdebug_debug_zval(&#39;e&#39;); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 结果如下 
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 remembers its own numberzval in PHP7Zval has a new implementation in PHP7. The most basic change is that the memory required by zval is no longer allocated separately from the heap, and the reference count is no longer stored by itself. The reference count of complex data types (such as strings, arrays, and objects) is stored by itself. This implementation has the following benefits: Simple data types do not need to allocate memory separately, and do not need to count There will no longer be two counts. In the object, there is only the object itself The stored count is validSince the count is now stored by the value itself, it can be shared with non-zval structure data, such as the pointer required for indirect access between zval and hashtable key The number has decreased #

<?php 
//php zval变量容器$a = 1;$b = 1;$c = &$a;$d = $b;$e = range(0, 3); 
xdebug_debug_zval(&#39;a&#39;);
 xdebug_debug_zval(&#39;b&#39;); 
xdebug_debug_zval(&#39;c&#39;);
 xdebug_debug_zval(&#39;d&#39;); 
xdebug_debug_zval(&#39;e&#39;); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 结果如下 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. Complex types such as arrays remember their own numbers. What is garbage?Only under guideline 3, the GC will collect zval and then pass the new Algorithm to determine whether this zval is garbage. So how to judge whether such a variable is real garbage? Simply put, it is to perform a refcount minus 1 operation for each element in this zval. After the operation is completed, if the refcount of zval=0, then this zval is a garbageIf If the refcount of a zval increases, then the zval is still in use and does not belong to garbage.If the refcount of a zval decreases to 0, then the zval can be released and does not belong to garbage.If a zval After the refcount is reduced and is greater than 0, then this zval cannot be released, and this zval may become garbage#

The above is the detailed content of Introduction to PHP garbage collection mechanism (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete