包含 php 5 與 php7 的變量實現和垃圾回收的對比php
PHP 的變量是弱類型的,能夠表示整數、浮點數、字符串等類型。PHP 的變量是使用結構體 zval 表示的node
PHP 5.* zval 和 zend_value 結構算法
struct _zval_struct { // 結構體 zvalue_value value; zend_uint refcount__gc; zend_uchar type; zend_uchar is_ref__gc; } typedef union _zvalue_value { // 聯合體 long lval; double dval; struct { char *val; int len; } str; // 字符串 HashTable *ht; // 數組 zend_object_value obj; // 對象 zend_ast *ast; } zvalue_value;
PHP 7.0 zval 和 zend_value 結構數組
struct _zval_struct { union { zend_long lval; /* long value */ double dval; /* double value */ zend_refcounted *counted; zend_string *str; zend_array *arr; zend_object *obj; zend_resource *res; zend_reference *ref; zend_ast_ref *ast; zval *zv; void *ptr; zend_class_entry *ce; zend_function *func; struct { uint32_t w1; uint32_t w2; } ww; } value; union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2; };
php 5.* 變量賦值等操做引用計數如圖所示,在倒數第二步,會造成一個循環引用,而且在 unset
操做以後,會產生垃圾。php7
PHP 7 的計數放到了具體的 value 中,zval 不存在寫時複製(寫時分離)。ui
而且 PHP 7 的有一個專門的 zend_reference
用來表示引用。.net
有了以上關於 PHP 變量存儲的知識,咱們能夠理解一下 PHP 是如何作垃圾回收的了。3d
首先,咱們須要定義什麼是垃圾。code
論文:https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf對象
PHP 5.3 版本以及以後的版本