加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中的引用计数 – 它是如何工作的?

发布时间:2020-12-13 22:51:31 所属栏目:PHP教程 来源:网络整理
导读:我想了解这篇文章“ PHP Manual - Features - Garbage Collection” 不幸的是,对我来说很少有事情不清楚. 1. To avoid having to call the checking of garbage cycles with every possible decrease of a refcount, the algorithm instead puts all possibl
我想了解这篇文章“ PHP Manual -> Features -> Garbage Collection”
不幸的是,对我来说很少有事情不清楚.

1.

To avoid having to call the checking of garbage cycles with every
possible decrease of a refcount,the algorithm instead puts all
possible roots (zvals) in the “root buffer”.

但是为了以防万一

<?php
$a = new stdClass(); (1)
$a = new stdClass();

然后我猜第一个对象就变成了“迷失”的zval

no_symbol : (refcount=1,is_ref=1) = stdObject

这些“丢失”的zval是否会被添加到根缓冲区中?他们没有处理程序.

2.

在函数范围内创建的变量,它们发生了什么?
例如:

<?php
function test($a = 'abc') {
    $c = 123;

    return 1; 
}

test(); 
echo 'end';

当gc启动时,$a和$c发生了什么?
这些变量仍将refcount设置为1.
他们还会被删除吗?如果是,那么为什么以及如何(封面下发生了什么?)

3.
它如何帮助循环引用?
防爆

<?php
$a = array('abc');
$a[] =& $a;
unset($a);

哪里

(refcount=1,is_ref=1)=array (
   0 => (refcount=1,is_ref=0)='abc',1 => (refcount=1,is_ref=1)=...
)

解决方法

1)原始对象被替换为新对象,因此可以立即释放内存.

echo memory_get_usage().'<br/>';
$a = new stdClass();
echo memory_get_usage().'<br/>';
$a = new stdClass();
echo memory_get_usage().'<br/>';

2)它们是第二个函数完成执行的gc’ed:

echo memory_get_usage().'<br/>';
function test($a = 'abc') {
    $c = 123;

    return 1;
}
echo memory_get_usage().'<br/>';
test();
echo memory_get_usage().'<br/>';

3)取消设置$a会将引用的变量留在内存中.您需要先将值设置为NULL,然后取消设置.

echo memory_get_usage().'<br/>';
$a = array('abc');
echo memory_get_usage().'<br/>';
$a[] =& $a;
echo memory_get_usage().'<br/>';
$a = null;
unset($a);
echo memory_get_usage().'<br/>';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读