Home  >  Article  >  Backend Development  >  In-depth explanation of PHP garbage collection and memory management related content

In-depth explanation of PHP garbage collection and memory management related content

藏色散人
藏色散人forward
2021-11-25 14:01:433737browse

This article will describe the garbage collection and memory management related content in the development process of PHP.

Reference Counting

In PHP 5.2 and previous versions, PHP’s garbage collection uses the Reference Counting algorithm.

Basic knowledge of reference counting

Basic knowledge of reference counting

The variables of php are stored in the "zval" variable container (data structure), "zval "The attribute contains the following information:

  • The data type of the current variable;
  • The value of the current variable;
  • is_ref Boolean type used to identify whether the variable is passed by reference Identification;
  • points to the refcount identifier of the number of variables in the "zval" variable container (that is, the number of times this zval has been referenced. Note that the reference here does not refer to pass-by-value, pay attention to the distinction).

When a variable is assigned a value, a corresponding "zavl" variable container will be generated. [Recommended learning: PHP video tutorial]

View variable zval container information

To view the "zval" container information of the variable (that is, view the is_ref and refcount of the variable), You can use the xdebug_debug_zval() function of the XDebug debugging tool.

For how to install the XDebug extension plug-in, you can view this tutorial (https://github.com/huliuqing/phpnotes/issues/58). For how to use XDebug, please read the official document (https://xdebug.org /docs/).

Assume that we have successfully installed the XDebug tool and can now debug variables.

  • View the zval information of ordinary variables

If our PHP statement is just a simple assignment of variables, the is_ref identifier value is 0 and the refcount value is 1; if When this variable is assigned as a value to another variable, the refcount count of the zval variable container is increased; similarly, when the variable is destroyed (unset), the "refcount" is subtracted by 1 accordingly.

Please see the following example:

<?php // 变量赋值时,refcount 值等于 1
$name = &#39;liugongzi&#39;;
xdebug_debug_zval(&#39;name&#39;); // (refcount=1, is_ref=0)string &#39;liugongzi&#39; (length=9)

// $name 作为值赋值给另一个变量, refcount 值增加 1
$copy = $name;
xdebug_debug_zval(&#39;name&#39;); // (refcount=2, is_ref=0)string &#39;liugongzi&#39; (length=9)

// 销毁变量,refcount 值减掉 1
unset($copy);
xdebug_debug_zval(&#39;name&#39;); // (refcount=1, is_ref=0)string &#39;liugongzi&#39; (length=9)
  • Copy on Write
Copy On Write: COW, simply described as: If When assigning a value to a variable, new memory is not allocated to store the value saved by the new variable. Instead, the memory is simply shared through a counter. New memory is allocated only when the value of one of the references pointing to the variable changes. Space to save value content to reduce memory usage. - TPIP copy-on-write

Through the previous zval information of simple variables, we know that $copy and $name share the zval variable container (memory), and then pass refcount to indicate how many variables are currently used by this zval.

Look at an example:

<?php $name = &#39;liugongzi&#39;;
xdebug_debug_zval(&#39;name&#39;); // name: (refcount=1, is_ref=0)string &#39;liugongzi&#39; (length=9)

$copy = $name;
xdebug_debug_zval(&#39;name&#39;); // name: (refcount=2, is_ref=0)string &#39;liugongzi&#39; (length=9)

// 将新的值赋值给变量 $copy
$copy = &#39;liugongzi handsome&#39;;
xdebug_debug_zval(&#39;name&#39;); // name: (refcount=1, is_ref=0)string &#39;liugongzi&#39; (length=9)
xdebug_debug_zval(&#39;copy&#39;); // copy: (refcount=1, is_ref=0)=&#39;liugongzi handsome&#39;

Did you notice that when the value liugongzi handsome is assigned to the variable $copy, the refcount values ​​​​of name and copy become 1. The following operations occur during this process:

  • Separate $copy from the zval (inner slave) of $name (that is, copy);
  • Subtract 1 from the refcount of $name;
  • Modify the zval of $copy (reassign and modify the refcount);

This is just a simple "copy-on-write" operation Introduction, interested friends can read the reference materials given at the end of the article for more in-depth research.

  • View the zval information of the variable passed by reference

The "reference counting" rules of reference pass-by-value (&) are the same as those of ordinary assignment statements, except that is_ref## The value of the # identifier is 1, indicating that the variable is a reference pass-by-value type.

Let’s now take a look at an example of passing by reference:

<?php $age = &#39;liugongzi&#39;;
xdebug_debug_zval(&#39;age&#39;); // (refcount=1, is_ref=0)string &#39;liugongzi&#39; (length=9)

$copy = &$age;
xdebug_debug_zval(&#39;age&#39;); // (refcount=2, is_ref=1)string &#39;liugongzi&#39; (length=9)

unset($copy);
xdebug_debug_zval(&#39;age&#39;); // (refcount=1, is_ref=1)string &#39;liugongzi&#39; (length=9)
    Reference counting of composite types
With scalar types (integer, floating point type, Boolean, etc.), the reference counting rules for types such as arrays and objects are slightly more complicated.

For a better explanation, let’s first look at the reference counting example of an array:

$a = array( 'meaning' => 'life', 'number' => 42 );
xdebug_debug_zval( 'a' );

// a:
// (refcount=1, is_ref=0)
// array (size=2)
//  'meaning' => (refcount=1, is_ref=0)string 'life' (length=4)
//  'number' => (refcount=1, is_ref=0)int 42
The reference counting diagram above is as follows:

In-depth explanation of PHP garbage collection and memory management related content

From the picture we find that the reference counting rules of composite types are basically the same as the counting rules of scalars. For the example given, PHP will create 3 zval variable containers, one for storing the array itself, and the other two for Store elements in an array.

When adding an existing element to the array, its reference counter refcount will be increased by 1.

$a = array( 'meaning' => 'life', 'number' => 42 );
xdebug_debug_zval( 'a' );
$a['life'] = $a['meaning'];
xdebug_debug_zval( 'a' );

// a:
// (refcount=1, is_ref=0)
// array (size=3)
//  'meaning' => (refcount=2, is_ref=0)string 'life' (length=4)
//  'number' => (refcount=0, is_ref=0)int 42
//  'life' => (refcount=2, is_ref=0)string 'life' (length=4)
The rough diagram is as follows:

In-depth explanation of PHP garbage collection and memory management related content

  • 内存泄露

虽然,复合类型的引用计数规则同标量类型大致相同,但是如果引用的值为变量自身(即循环应用),在处理不当时,就有可能会造成内存泄露的问题。

让我们来看看下面这个对数组进行引用传值的示例:

<?php // @link http://php.net/manual/zh/function.memory-get-usage.php#96280
function convert($size)
{
    $unit=array(&#39;b&#39;,&#39;kb&#39;,&#39;mb&#39;,&#39;gb&#39;,&#39;tb&#39;,&#39;pb&#39;);
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).&#39; &#39;.$unit[$i];
}

// 注意:有用的地方从这里开始
$memory = memory_get_usage();

$a = array( &#39;one&#39; );

// 引用自身(循环引用)
$a[] =&$a;

xdebug_debug_zval( &#39;a&#39; );

var_dump(convert(memory_get_usage() - $memory)); // 296 b

unset($a); // 删除变量 $a,由于 $a 中的元素引用了自身(循环引用)最终导致 $a 所使用的内存无法被回收

var_dump(convert(memory_get_usage() - $memory)); // 568 b

从内存占用结果上看,虽然我们执行了 unset($a) 方法来销毁 $a 数组,但内存并没有被回收,整个处理过程的示意图如下:

In-depth explanation of PHP garbage collection and memory management related content

可以看到对于这块内存,再也没有符合表(变量)指向了,所以 PHP 无法完成内存回收,官方给出的解释如下:

尽管不再有某个作用域中的任何符号指向这个结构 (就是变量容器),由于数组元素 “1” 仍然指向数组本身,所以这个容器不能被清除 。因为没有另外的符号指向它,用户没有办法清除这个结构,结果就会导致内存泄漏。庆幸的是,php 将在脚本执行结束时清除这个数据结构,但是在 php 清除之前,将耗费不少内存。如果你要实现分析算法,或者要做其他像一个子元素指向它的父元素这样的事情,这种情况就会经常发生。当然,同样的情况也会发生在对象上,实际上对象更有可能出现这种情况,因为对象总是隐式的被引用。 - 摘自 官方文档 Cleanup Problems

简单来说就是「引用计数」算法无法检测并释放循环引用所使用的内存,最终导致内存泄露。

引用计数系统的同步周期回收

由于引用计数算法存在无法回收循环应用导致的内存泄露问题,在 PHP 5.3 之后对内存回收的实现做了优化,通过采用 引用计数系统的同步周期回收 算法实现内存管理。引用计数系统的同步周期回收算法是一个改良版本的引用计数算法,它在引用基础上做出了如下几个方面的增强:

  • 引入了可能根(possible root)的概念:通过引用计数相关学习,我们知道如果一个变量(zval)被引用,要么是被全局符号表中的符号引用(即变量),要么被复杂类型(如数组)的 zval 中的符号(数组的元素)引用,那么这个 zval 变量容器就是「可能根」。
  • 引入根缓冲区(root buffer)的概念:根缓冲区用于存放所有「可能根」,它是固定大小的,默认可存 10000 个可能根,如需修改可以通过修改 PHP 源码文件 Zend/zend_gc.c 中的常量 GC_ROOT_BUFFER_MAX_ENTRIES,再重新编译。
  • 回收周期:当缓冲区满时,对缓冲区中的所有可能根进行垃圾回收处理。

下图(来自 PHP 手册),展示了新的回收算法执行过程:

In-depth explanation of PHP garbage collection and memory management related content

引用计数系统的同步周期回收过程

  1. 缓冲区(紫色框部分,称为疑似垃圾),存储所有可能根(步骤 A);
  2. 采用深度优先算法遍历「根缓冲区」中所有的「可能根(即 zval 遍历容器)」,并对每个 zval 的 refcount 减 1,为了避免遍历时对同一个 zval 多次减 1(因为不同的根可能遍历到同一个 zval)将这个 zvel 标记为「已减」(步骤 B);
  3. 再次采用深度优先遍历算法遍历「可能根 zval」。当 zval 的 refcount 值不为 0 时,对其加 1,否则保持为 0。并请已遍历的 zval 变量容器标记为「已恢复」(即步骤 B 的逆运算)。那些 zval 的 refcount 值为 0 (蓝色框标记)的就是应该被回收的变量(步骤 C);
  4. 删除所有 refcount 为 0 的可能根(步骤 D)。

整个过程为:

采用深度优先算法执行:默认删除 > 模拟恢复 > 执行删除 达到内存回收的目的。

优化后的引用计数算法优势

  • 将内存泄露控制在阀值内,这个由缓存区实现,达到缓冲区大小执行新一轮垃圾回收;
  • 提升了垃圾回收性能,不是每次 refcount 减 1 都执行回收处理,而是等到根缓冲区满时才开始执行垃圾回收。

你可以从 PHP 手册 的回收周期 了解更多,也可以阅读文末给出的参考资料。

PHP 7 的内存管理

PHP 5 中 zval 实现上的主要问题:

  • zval Always allocate memory separately from the heap;
  • zval Always store reference counting and recycling information, even for integers (bool/null) This kind of data may not require such information;
  • When using objects or resources, direct references will result in double counting;
  • Some indirect access requires a Better way to handle it. For example, accessing objects stored in variables now indirectly uses four pointers (the length of the pointer chain is four);
  • Direct counting means that values ​​can only be shared between zvals. This doesn't work if you want to share a string between a zval and a hashtable key (unless the hashtable key is also a zval).

Adjustments to the zval data structure implementation in PHP 7:

The most basic change is that the memory required by zval is no longer allocated separately from the heap. The reference count is no longer stored by zval.
The reference count of complex data types (such as strings, arrays, and objects) is stored by itself. - Excerpted from Internal value representation in PHP 7 - Part 1【Translation】

Advantages of this implementation:

  • Simple data types do not need to allocate memory separately and do not need to be counted;
  • There will be no more double counting. In an object, only the count stored in the object itself is valid;
  • Since the count is now stored by the value itself (PHP has zval variable container storage), it can be shared with data in non-zval structures, such as zval and hashtable key;
  • The number of pointers required for indirect access is reduced.

For more specific details about PHP 7 zval implementation and memory optimization, you can read In-depth understanding of zval and Internal value representation in PHP 7 kernel - Part 1 translation. (https://www.npopov.com/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html)

The above is the detailed content of In-depth explanation of PHP garbage collection and memory management related content. For more information, please follow other related articles on the PHP Chinese website!

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