How does PHP's 'unset' construct work internally?

随声附和 提交于 2019-11-29 14:15:16
bwoebi

TL;DR

Both statements are true.

Let me explain. (It's true since at least PHP 5.0 (before, I don't know). There comes phpng now, which does fundamental changes, but this principle is still used.)


The cirular garbage collector

The circular garbage collector is just used for circular references. We have them usually when two objects contain references to each other.

As in this case the refcount__gc would never drop to zero… there's still some reference elsewhere, the normal ZEND_UNSET_* (where the asterisk is either ARRAY, OBJ or VAR) cannot unset it. So it has to wait for the garbage collector.

And the garbage collector is only called periodically for performance reasons.

php-src definitions

You asked for the definition of the ZEND_UNSET_VAR? http://lxr.php.net/xref/PHP_5_6/Zend/zend_vm_def.h#4069

And here is the main function for decrementing refcount etc.: http://lxr.php.net/xref/PHP_5_6/Zend/zend_execute.h#74

Which one is correct?

So, if refcount is zero, we are sure that nothing links to it and we can free it. (Second statement: is just talking about the refcount == 0 case)

But, if it's not zero, we mark the variable as to be checked by the circular garbage collector later. (First statement: not necessarily immediately freed)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!