How PHP does garbage collection

Posted by benyamin on Wed, 13 Nov 2019 20:14:37 +0100

Comparison of variable implementation and garbage collection with php 5 and php7

Variable implementation

PHP variables are of weak type, which can represent integer, floating-point number, string and other types. PHP variables are represented by the structure zval

PHP 5.* zval and zend_value structures

struct _zval_struct { // structural morphology
    zvalue_value value;
    zend_uint refcount__gc;
    zend_uchar type;
    zend_uchar is_ref__gc;
}

typedef union _zvalue_value { // Consortium
    long lval;
    double dval;
    struct {
        char *val;
        int len;
    } str; // Character string
    HashTable *ht; // array
    zend_object_value obj; // object
    zend_ast *ast;
} zvalue_value;

PHP 7.0 zval and zend_value structures

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

Reference counting

php 5. * operation reference count such as variable assignment is shown in the figure:

PHP 7.0

What is rubbish

  1. refcount is not added
  2. If refcount = 0, this will be cleared directly
  3. refcount is reduced, and the one with! = 0 is garbage

garbage collection

  1. php7 requires that the data type is array and object, and is "type" collectable
  2. Not in buffer
  3. Not marked
  4. Mark it purple and put it in the buffer

Recovery algorithm

Papers: https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf

PHP 5.3 and later

  1. Put garbage in a root pool
  2. Garbage collection when 10000 nodes are full
  3. Traversing the node refcount-1 in the bidirectional linked list
  4. Traverse the two-way linked list to delete the node with refcount=0
  5. refcount+1 for refcount!=0

Topics: PHP