PHP引用计数

1. PHP官方手册引用计数介绍

引用计数
每个php变量存在一个叫"zval"的变量容器中。一个zval变量容器,除了包含变量的类型和值,还包括两个字节的额外信息。第一个是"is_ref",是个bool值,用来标识这个变量是否是属于引用集合(reference set)。通过这个字节,php引擎才能把普通变量和引用变量区分开来,由于php允许用户通过使用&来使用自定义引用,zval变量容器中还有一个内部引用计数机制,来优化内存使用。第二个额外字节是"refcount",用以表示指向这个zval变量容器的变量(也称符号即symbol)个数。所有的符号存在一个符号表中,其中每个符号都有作用域(scope),那些主脚本(比如:通过浏览器请求的的脚本)和每个函数或者方法也都有作用域。

2. 测试

2.1 当a是string


文档下方demo的xdebug测试结果

result:

a: (refcount=2, is_ref=0)='new string'

同样的代码我在php7 下的xdebug测试结果

result

a: (refcount=0, is_ref=0)='new string'

居然a是不计数的!!!跟文档不一样!!!

2.2 a是类

我知道php7重写了zval,但不知道为什么这么搞,手痒试了一下a是int类型的时候,发现和string是一样的,又写了a是类的情况:


result:

a: (refcount=2, is_ref=0)=class A { public $a = (refcount=2, is_ref=0)='A' }
b: (refcount=2, is_ref=1)=class A { public $a = (refcount=2, is_ref=0)='A' }

发现a是类的时候又是计数的。

3. 网上找解释

stackoverflow的讨论
In PHP 7 a zval can be reference counted or not. There is a flag in the zval structure which determined this.

There are some types which are never refcounted. These types are null, bool, int and double.

There are other types which are always refcounted. These are objects, resources and references.

And then there are types, which are sometimes refcounted. Those are strings and arrays.

For strings the not-refcounted variant is called an "interned string". If you're using an NTS (not thread-safe) PHP 7 build, which you typically are, all string literals in your code will be interned. These interned strings are deduplicated (i.e. there is only one interned string with a certain content) and are guaranteed to exist for the full duration of the request, so there is no need to use reference counting for them. If you use opcache, these strings will live in shared memory, in which case you can't use reference counting for them (as our refcounting mechanism is non-atomic). Interned strings have a dummy refcount of 1, which is what you're seeing here.

For arrays the not-refcounted variant is called an "immutable array". If you use opcache, then constant array literals in your code will be converted into immutable arrays. Once again, these live in shared memory and as such must not use refcounting. Immutable arrays have a dummy refcount of 2, as it allows us to optimize certain separation paths.

意思就是php7中refcounted只针对某些变量类型,null、bool、int、double这些不计数,objects、resources、references计数。

那些不用计数的string我们称为"interned string",如果你用的是php7的非线程安全模式,那么这些相同的串其实都是同一引用值,并且这些值的生命周期只是一次请求,所以对它们计数没什么意义。假设你用了opcode缓存,这些strings将在共享内存,在这种情况下你不能进行引用计数(因为我们的计数原理是非原子操作的)。

  • 补充
不是所有类型都可以copy的,比如对象、资源,事实上只有string、array两种支持,与引用计数相同,也是通过zval.u1.type_flag标识value是否可复制的:

#define IS_TYPE_COPYABLE         (1<<4)
|     type       |  copyable  |
+----------------+------------+
|simple types    |            |
|string          |      Y     |
|interned string |            |
|array           |      Y     |
|immutable array |            |
|object          |            |
|resource        |            |
|reference       |            |
copyable 的意思是当value发生duplication时是否需要或者能够copy,这个具体有两种情形下会发生:

a.从 literal变量区 复制到 局部变量区 ,比如:$a = [];实际会有两个数组,而$a = "hi~";//interned string则只有一个string
b.局部变量区分离时(写时复制):如改变变量内容时引用计数大于1则需要分离,$a = [];$b = $a; $b[] = 1;这里会分离,类型是array所以可以复制,如果是对象:$a = new user;$b = $a;$a->name = "dd";这种情况是不会复制object的,$a、$b指向的对象还是同一个
具体literal、局部变量区变量的初始化、赋值后面编译、执行两篇文章会具体分析,这里知道变量有个copyable的属性就行了。

4. [附录]xdebug配置

http://xdebug.org/install.php#configure-php
http://blog.jetbrains.com/phpstorm/2013/08/debugger-configuration-validation-with-phpstorm/

on CentOS:

1. Compile Xdebug, pecl需要php7,在php安装目录的bin文件夹下执行:
./pecl install Xdebug

2. Find the php.ini file using
locate php.ini
And add the following line
[xdebug]
zend_extension="/usr/lib64/php/modules/xdebug.so"
xdebug.remote_enable = 1

3. Restart Apache 或php-fpm
service httpd restart

4. Test if it works – create test.php with the following code

你可能感兴趣的:(PHP引用计数)