php waste disposal mechanism

Before php5.3 versions, variable recovery mechanisms php simply processes (when refcount = 0, reclaims memory) by counting, but this will be a problem

$a=array("str");

$a[]=&$a;

unset($a);

Before performing unset, $ a refcount of 2, after performing unset, $ a is refcout 1, because it is not equal to 0, the memory can not be recovered, i.e. waste, of course, after a php script completes the assigned memory will all be recycled, but now used in addition to the php script, more places for writing a daemon service (of course I do not recommend), may take up to a month, two months before the end of the script, such as the above procedures during this period It will produce memory overflow

 Note: unset and can not release memory, you need to look at the zval refcount is 0

php5.3 later by the GC garbage collection

When allocating zval, to zval_gc_info units

 

Copy the code
#define ALLOC_ZVAL(z)                                     \
    do {                                                \
        (z) = (zval*)emalloc(sizeof(zval_gc_info));        \
        GC_ZVAL_INIT(z);                                \
    } while (0)

typedef struct _zval_gc_info {
    zval z;
    union {
        gc_root_buffer       *buffered;
        struct _zval_gc_info *next;
    } u;
} zval_gc_info;

#define FREE_ZVAL(z)                                     \
    do {                                                \
        GC_REMOVE_ZVAL_FROM_BUFFER(z);                    \
        efree(z);                                        \
    } while (0)
Copy the code

 

 

Copy the code
typedef struct _gc_root_buffer {
    struct _gc_root_buffer   *prev;        /* double-linked list               */
    struct _gc_root_buffer   *next;
    zend_object_handle        handle;    /* must be 0 for zval               */
    union {
        zval                 *pz;
        zend_object_handlers *handlers;
    } u;
} gc_root_buffer;
Copy the code

 

 

 

Php when the GC recovery mechanism starts, it allocates space of 10,000 gc_root_buffer

Copy the code
ZEND_API void gc_init(TSRMLS_D)  
{  
    if (GC_G(buf) == NULL && GC_G(gc_enabled)) {  
        GC_G(buf) = (gc_root_buffer*) malloc(sizeof(gc_root_buffer) * GC_ROOT_BUFFER_MAX_ENTRIES);  
        GC_G(last_unused) = &GC_G(buf)[GC_ROOT_BUFFER_MAX_ENTRIES];  
        gc_reset(TSRMLS_C);  
    }  
}  
Copy the code

 

When the unset ($ a), detailed here  after a key is found in the zval active_systom_table, performs its value destructor, which was the zval refcount-1, 1 if the value is 0 after subtraction, immediate release may be described memory, if it is greater than 0, in place gc_root_buffer

Copy the code
ZEND_API void _zval_ptr_dtor(zval **zval_ptr ZEND_FILE_LINE_DC) /* {{{ */  
{   
    Z_DELREF_PP(zval_ptr);  
    if (Z_REFCOUNT_PP(zval_ptr) == 0) {  
        TSRMLS_FETCH();  
  
        if (*zval_ptr != &EG(uninitialized_zval)) {  
            GC_REMOVE_ZVAL_FROM_BUFFER(*zval_ptr);  
            zval_dtor(*zval_ptr);  
            efree_rel(*zval_ptr);  
        }  
    } else {  
        TSRMLS_FETCH();  
  
        if (Z_REFCOUNT_PP(zval_ptr) == 1) {  
            Z_UNSET_ISREF_PP(zval_ptr);  
        }  
  
        GC_ZVAL_CHECK_POSSIBLE_ROOT(*zval_ptr);  
    }  
}  
Copy the code

 

 

 

Copy the code
#define GC_ZVAL_CHECK_POSSIBLE_ROOT(z) /
    gc_zval_check_possible_root((z) TSRMLS_CC)

static zend_always_inline void gc_zval_check_possible_root(zval *z TSRMLS_DC)
{
    if (z->type == IS_ARRAY || z->type == IS_OBJECT) {
        gc_zval_possible_root(z TSRMLS_CC);
    }
}

ZEND_API void gc_zval_possible_root(zval *zv TSRMLS_DC)
{
    if (UNEXPECTED(GC_G(free_list) != NULL &&
                   GC_ZVAL_ADDRESS(zv) != NULL &&
                   GC_ZVAL_GET_COLOR(zv) == GC_BLACK) &&
                   (GC_ZVAL_ADDRESS(zv) < GC_G(buf) ||
                    GC_ZVAL_ADDRESS(zv) >= GC_G(last_unused))) {
        / * A Garbage of The IS GIVEN that the zval IS deleted by going to BE 
         * Currently running the GC * / 
        return; 
    } 

    IF (zv-> type == IS_OBJECT) { 
        GC_ZOBJ_CHECK_POSSIBLE_ROOT (ZV); 
        return; 
    } 

    GC_BENCH_INC (zval_possible_root); // zv gc_root_buffer If the last two are not purple, then processed
     IF (GC_ZVAL_GET_COLOR (zv) = GC_PURPLE!) { 
        GC_ZVAL_SET_PURPLE (zv); // set purple IF {(GC_ZVAL_ADDRESS (zv)!) 
            gc_root_buffer * = newRoot GC_G ( unused); 
            IF (newRoot) { 
                GC_G (unused) = newRoot-> PREV; 
            !} the else IF (GC_G (first_unused) = GC_G (last_unused)) {
    

        

                newRoot = GC_G(first_unused);
                GC_G(first_unused)++;
            } else {
                if (!GC_G(gc_enabled)) {
                    GC_ZVAL_SET_BLACK(zv);
                    return;
                }
                zv->refcount__gc++;
                gc_collect_cycles(TSRMLS_C);
                zv->refcount__gc--;
                newRoot = GC_G(unused);
                if (!newRoot) {
                    return;
                }
                GC_ZVAL_SET_PURPLE(zv);
                GC_G(unused) = newRoot->prev;
            }

            newRoot->next = GC_G(roots).next;
            newRoot->prev = &GC_G(roots);
            GC_G(roots).next->prev = newRoot;
            GC_G(roots).next = newRoot;

            GC_ZVAL_SET_ADDRESS(zv, newRoot); //将gc_root_buffer放到zval_gc_info结构体中

            newRoot->handle = 0;
            newRoot->u.pz = zv;

            GC_BENCH_INC(zval_buffered);
            GC_BENCH_INC(root_buf_length);
            GC_BENCH_PEAK(root_buf_peak, root_buf_length);
        }
    }
}
Copy the code

 

 

The current zval into gc_root_bufer, each zval put only once, based on whether the color gc_root_buffer of zval_gc_info zval located in the purple

 

#define GC_ZVAL_GET_COLOR(v) \
GC_GET_COLOR(((zval_gc_info*)(v))->u.buffered)

#define GC_GET_COLOR(v) \
(((zend_uintptr_t)(v)) & GC_COLOR)

 

If it is purple, then to purple

#define GC_ZVAL_SET_PURPLE(v) \
GC_SET_PURPLE(((zval_gc_info*)(v))->u.buffered)

#define GC_SET_PURPLE(v) \
(v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) | GC_PURPLE))

 

Macro GC_ZVAL_SET_ADDRESS (zv, newRoot); gc_root_buffer newRoot used to place the corresponding position zv

Copy the code
#define GC_COLOR  0x03

#define GC_BLACK  0x00
#define GC_WHITE  0x01
#define GC_GREY   0x02
#define GC_PURPLE 0x03

#define GC_ZVAL_SET_ADDRESS(v, a) \
    GC_SET_ADDRESS(((zval_gc_info*)(v))->u.buffered, (a))

#define GC_SET_ADDRESS(v, a) \
    (v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & GC_COLOR) | ((zend_uintptr_t)(a))))
Copy the code

 

 

GC_ZVAL_SET_ADDRESS macro v forced into zval_gc_info first type, itself zval when allocating memory, is to zval_gc_info units, into the strong zval *, since only zval padding data structure, this need not gc_root_buffer * bufer like members 
because zval_gc_val structure, the first member of the zval z, z, then the memory address is the address itself zval_gc_info

in PHP GC using colors to indicate the waste processing

pointer in both 32-bit machine or a 64-bit machine The last two are 0,

gc_collect_cycles waste disposal

 


Copy the code
ZEND_API int gc_collect_cycles(TSRMLS_D)
{
    int count = 0;

    if (GC_G(roots).next != &GC_G(roots)) {
        zval_gc_info *p, *q, *orig_free_list, *orig_next_to_free;

        if (GC_G(gc_active)) {
            return 0;
        }
        GC_G(gc_runs)++;
        GC_G(zval_to_free) = FREE_LIST_END;
        GC_G(gc_active) = 1;
        gc_mark_roots(TSRMLS_C);
        gc_scan_roots(TSRMLS_C);
        gc_collect_roots(TSRMLS_C);

        orig_free_list = GC_G(free_list);
        orig_next_to_free = GC_G(next_to_free);
        p = GC_G(free_list) = GC_G(zval_to_free);
        GC_G(zval_to_free) = NULL;
        GC_G(gc_active) = 0;

        /* First call destructors */
        while (p != FREE_LIST_END) {
            if (Z_TYPE(p->z) == IS_OBJECT) {
                if (EG(objects_store).object_buckets &&
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].valid &&
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount <= 0 &&
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.dtor &&
                    !EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].destructor_called) {

                      EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].destructor_called = 1;
                      EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount++;
                      EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.dtor(EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.object, Z_OBJ_HANDLE(p->z) TSRMLS_CC);
                      EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount--;
                  }
            }
            count++;
            p = p->u.next;
        }

        /* Destroy zvals */
        p = GC_G(free_list);
        while (p != FREE_LIST_END) {
            GC_G(next_to_free) = p->u.next;
            if (Z_TYPE(p->z) == IS_OBJECT) {
                if (EG(objects_store).object_buckets &&
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].valid &&
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount <= 0) {
                    EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount = 1;
                    Z_TYPE(p->z) = IS_NULL;
                    zend_objects_store_del_ref_by_handle_ex(Z_OBJ_HANDLE(p->z), Z_OBJ_HT(p->z) TSRMLS_CC);
                }
            } else if (Z_TYPE(p->z) == IS_ARRAY) {
                Z_TYPE(p->z) = IS_NULL;
                zend_hash_destroy(Z_ARRVAL(p->z));
                FREE_HASHTABLE(Z_ARRVAL(p->with));
            } else {
                zval_dtor(&p->z);
                Z_TYPE(p->z) = IS_NULL;
            }
            p = GC_G(next_to_free);
        }

        /* Free zvals */
        p = GC_G(free_list);
        while (p != FREE_LIST_END) {
            q = p->u.next;
            FREE_ZVAL_EX(&p->z);
            p = q;
        }
        GC_G(collected) += count;
        GC_G(free_list) = orig_free_list;
        GC_G(next_to_free) = orig_next_to_free;
    }

    return count;
}
Copy the code

 

 
gc_mark_roots (TSRMLS_C); spam lay color marker, traverse gc_root_buffer, which was changed to purple to gray u.pz traversing u.pz each element of the array zval,. 1-which the refcount, 
gc_collect_roots traverse gc_root_buffer, if refcount = = 0 then set to white, expressed as spam, if refcount> 0, indicate someone may be using, set to black
 
Copy the code
static void gc_mark_roots(TSRMLS_D)
{
    gc_root_buffer *current = GC_G(roots).next;

    while (current != &GC_G(roots)) {
        if (current->handle) {
            if (EG(objects_store).object_buckets) {
                //处理对象,暂时不用看
            }
        } else {
            if (GC_ZVAL_GET_COLOR(current->u.pz) == GC_PURPLE) {
                zval_mark_grey(current->u.pz TSRMLS_CC);
            } else {
                GC_ZVAL_SET_ADDRESS(current->u.pz, NULL);
                GC_REMOVE_FROM_BUFFER(current);
            }
        }
        current = current->next;
    }
}
Copy the code
 

 

 
Copy the code
static void zval_mark_grey(zval *pz TSRMLS_DC)
{
    Bucket *p;

tail_call:
    if (GC_ZVAL_GET_COLOR(pz) != GC_GREY) {
        p = NULL;
        GC_BENCH_INC(zval_marked_grey);
        GC_ZVAL_SET_COLOR(pz, GC_GREY);

        if (Z_TYPE_P(pz) == IS_OBJECT && EG(objects_store).object_buckets) {
            //对象的处理, 暂时不用管
        } else if (Z_TYPE_P(pz) == IS_ARRAY) {
            if (Z_ARRVAL_P(pz) == &EG(symbol_table)) {
                GC_ZVAL_SET_BLACK(pz);
            } else {
                p = Z_ARRVAL_P(pz)->pListHead;
            }
        }
        while (p != NULL) {
            pz = *(zval**)p->pData;
            if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
                pz->refcount__gc--;
            }
            if (p->pListNext == NULL) {
                goto tail_call;
            } else {
                zval_mark_grey(pz TSRMLS_CC);
            }
            p = p->pListNext;
        }
    }
}
Copy the code

 

 The second pass gc_root_buffer, zv if the color is gray, and refcount = 0, then set to white, when refcount> 0, is set to black (not spam)

Copy the code
static void gc_scan_roots(TSRMLS_D)
{
    gc_root_buffer *current = GC_G(roots).next;

    while (current != &GC_G(roots)) {
        if (current->handle) {
            zval z;

            INIT_PZVAL(&z);
            Z_OBJ_HANDLE(z) = current->handle;
            Z_OBJ_HT(z) = current->u.handlers;
            zobj_scan(&z TSRMLS_CC);
        } else {
            zval_scan(current->u.pz TSRMLS_CC);
        }
        current = current->next;
    }
}
Copy the code

 

 

Copy the code
static int zval_scan(zval *pz TSRMLS_DC)
{
    Bucket *p;

tail_call:    
    if (GC_ZVAL_GET_COLOR(pz) == GC_GREY) {
        p = NULL;
        if (pz->refcount__gc > 0) {
            zval_scan_black(pz TSRMLS_CC);
        } else {
            GC_ZVAL_SET_COLOR(pz, GC_WHITE);
            if (Z_TYPE_P(pz) == IS_OBJECT && EG(objects_store).object_buckets) {
               //处理object
            } else if (Z_TYPE_P(pz) == IS_ARRAY) {
                if (Z_ARRVAL_P(pz) == &EG(symbol_table)) {
                    GC_ZVAL_SET_BLACK(pz);
                } else {
                    p = Z_ARRVAL_P(pz)->pListHead;
                }
            }
        }
        while (p != NULL) {
            if (p->pListNext == NULL) {
                pz = *(zval**)p->pData;
                goto tail_call;
            } else {
                zval_scan(*(zval**)p->pData TSRMLS_CC);
            }
            p = p->pListNext;
        }
    }
    return 0;
}
Copy the code

 

 

Gc_root_buffer linked list traversal, the placement of a single color zv list is white data, full recovery

Copy the code
static void gc_collect_roots(TSRMLS_D)
{
    gc_root_buffer *current = GC_G(roots).next;

    while (current != &GC_G(roots)) {
        if (current->handle) {
            if (EG(objects_store).object_buckets) {
                struct _store_object *obj = &EG(objects_store).object_buckets[current->handle].bucket.obj;
                zval z;

                GC_SET_ADDRESS(obj->buffered, NULL);
                INIT_PZVAL(&z);
                Z_OBJ_HANDLE(z) = current->handle;
                Z_OBJ_HT(z) = current->u.handlers;
                zobj_collect_white(&z TSRMLS_CC);
            }
        } else {
            GC_ZVAL_SET_ADDRESS(current->u.pz, NULL);
            zval_collect_white(current->u.pz TSRMLS_CC);
        }

        GC_REMOVE_FROM_BUFFER(current);
        current = current->next;
    }
}
Copy the code

 

 

Copy the code
static void zval_collect_white(zval *pz TSRMLS_DC)
{
    Bucket *p;

tail_call:
    if (((zval_gc_info*)(pz))->u.buffered == (gc_root_buffer*)GC_WHITE) {
        p = NULL;
        GC_ZVAL_SET_BLACK(pz);

        if (Z_TYPE_P(pz) == IS_OBJECT && EG(objects_store).object_buckets) {
            ...
        } else {
            if (Z_TYPE_P(pz) == IS_ARRAY) {
                p = Z_ARRVAL_P(pz)->pListHead;
            }
        }

        /* restore refcount and put into list to free */
        pz->refcount__gc++;
        ((zval_gc_info*)pz)->u.next = GC_G(zval_to_free);
        GC_G(zval_to_free) = (zval_gc_info*)pz;

        while (p != NULL) {
            pz = *(zval**)p->pData;
            if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
                pz->refcount__gc++;
            }
            if (p->pListNext == NULL) {
                goto tail_call;
            } else {
                zval_collect_white(pz TSRMLS_CC);
            }
            p = p->pListNext;
        }
    }
}
Copy the code

Guess you like

Origin www.cnblogs.com/yangjinqiang/p/11129308.html