PHP 是如何作垃圾回收的

PHP 是如何作垃圾回收的

包含 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;
};

PHP5 與 PHP7 引用計數的對比

php 5.* 變量賦值等操做引用計數如圖所示,在倒數第二步,會造成一個循環引用,而且在 unset 操做以後,會產生垃圾。php7

PHP 7 的計數放到了具體的 value 中,zval 不存在寫時複製(寫時分離)。ui

而且 PHP 7 的有一個專門的 zend_reference 用來表示引用。.net

垃圾回收.draw.io-PHP7

有了以上關於 PHP 變量存儲的知識,咱們能夠理解一下 PHP 是如何作垃圾回收的了。3d

什麼是垃圾

首先,咱們須要定義什麼是垃圾。code

  1. refcount 增長的不是
  2. refcount 等於0的不是,這個會被直接清除
  3. refcount 減小,而且不等於0的纔是垃圾

垃圾收集

  1. php7 要求數據類型是數組和對象,而且 type_flag 是 IS_TYPE_COLLECTABLE
  2. 沒有在緩衝區中存在過
  3. 沒有被標記過
  4. 標記爲紫色,而且放到緩衝區中

回收算法

論文:https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf對象

PHP 5.3 版本以及以後的版本

  1. 將垃圾放到一個 root 池中
  2. 當滿 10000 個節點的時候進行垃圾回收
  3. 遍歷雙向鏈表中的節點 refcount-1
  4. 遍歷雙向鏈表將 refcount=0 的節點刪除,到free隊列中
  5. 對 refcount!=0 的 refcount+1

四色切換

相關文章
相關標籤/搜索