教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php zval中的refcount问题详解

php zval中的refcount问题详解

发布时间:2017-12-08   编辑:jiaochengji.com
教程集为您提供php zval中的refcount问题详解等资源,欢迎您收藏本站,我们将为您提供最新的php zval中的refcount问题详解资源
下面我们来看一篇关于php zval中的refcount问题详解 ,希望这篇文章能够让各位理解到php zval中的refcount一些问题哦,具体的如下文介绍。


今天群里面的一位小伙伴问我关于PHP变量中refcount的问题,当时回答给他了,发现自己没注意到一个细节,记录下来。

什么是refcount

“zval结构体中有四个字段,其含义分别为:

属性名 含义 默认值
refcount__gc 表示引用计数 1
is_ref__gc 表示是否为引用 0
value 存储变量的值
type 变量具体的类型”

摘录来自: Reeze Xia. “TIPI: 深入理解PHP内核”。

refcount是php用来对zval变量引用次数技术的一个变量值。

它是一个计数器,用来保存有多少符号表有多少符号指向该zval。在变量生成时,其refcount=1,赋值操作$a = $b会令zval的refcount加1,zval的refcount如果减少到0,会释放该zval所占的内存空间。

问题是什么?

小伙伴原问题是为什么下面的计数会是3.

<center></center>

因为照上面的说法zval $a在被赋值为100的时候应该为1,$b = $a 的时候应该再加1,那么debug_zval_dump()函数照理来说应该显示2,而不应该显示3。

当时我的回答是,初始化的时候值为1,赋值的时候再加1,赋值给$b的时候再加1,所以是3。
但是后来他提出,使用xdebug_zval_dump()出来refcount的值是2。
那么,我之前的答案就是错的,惭愧,学艺不精。

于是我找了一些资料来研究两个函数之间处理的差异。

下面就是差异的原因。

PHP's debug_zval_dump takes a variable as argument for analysis and is thus also bound to the same splitting rules as outlined earlier. This means that's not really well suited for dumping a zval's internal structure as it would always modify the zval. Besides adding 1 to the refcount field, it could also force a split resulting in unexpected output:

<?php
$a = 42;
debug_zval_dump($a);
?>

shows:

long(42) refcount(2)

Xdebug has a similar function to display a zval's internal data: xdebug_debug_zval. This function does requires not a variable to be passed as its argument, but instead requires a variable name to be passed in. With this, the manipulation of the zval is avoided and the proper values are shown:

<?php
$a = 42;
xdebug_debug_zval('a');
?>

which shows:

a: (refcount=1, is_ref=0)=42

所以单纯的$a的refcount=0,赋值操作会 1

您可能感兴趣的文章:
php zval中的refcount问题详解
PHP垃圾回收机制分析
通过实例详细讲解PHP垃圾回收机制
不要在循环体中使用array_push()
PHP7 变量在内部的实现详解
不要在循环体中使用 array_push ()
php垃圾回收机制实例介绍
php垃圾回收机制(gc)介绍
PHP垃圾回收机制详解
PHP是如何做垃圾回收的(图文)

[关闭]
~ ~