Home > Backend Development > PHP7 > Detailed explanation of php7 garbage collection mechanism

Detailed explanation of php7 garbage collection mechanism

藏色散人
Release: 2023-02-17 12:32:01
forward
8061 people have browsed it

Detailed explanation of php7 garbage collection mechanism

php7 Detailed explanation of garbage collection mechanism

The author was interested in this topic a few days ago, so I searched online , almost all of them are the garbage collection mechanism of php 5. Although the changes made in the GC part from php5 to php7 are relatively small, I think it is still necessary to do a separate blog post. Unless otherwise stated, the PHP version is 7.2

. The space occupied by variables in PHP does not need to be reclaimed manually. The kernel handles this part of the work for us. Compared with C, this greatly facilitates our operation.

This article mainly explains the GC mechanism of variables

When understanding our php GC, I feel it is necessary to introduce the underlying implementation of our php variables.

The structure of zval

// php 变量对于的c结构体
struct _zval_struct {
    zend_value value;
    union {
       ……
    } u1;
    union {
        ……
    } u2;
};
Copy after login

Since it mainly talks about garbage collection, here is a brief introduction to the functions of the u1 u2 union

u1 structure is more complicated , I think it is mainly used to identify variable types

u2 Most of them are auxiliary fields, the implementation of internal functions of variables, improving cache friendliness, etc.

The next is our protagonist

zend_value It is also a union embedded in the structure

typedef union _zend_value {
    zend_long         lval;//整形
    double            dval;//浮点型
    zend_refcounted  *counted;//获取不同类型的gc头部
    zend_string      *str;//string字符串
    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 {
        ZEND_ENDIAN_LOHI(
            uint32_t w1,
            uint32_t w2)
    } ww;
} zend_value;
Copy after login

The reference count zend_refcounted *counted type is recorded in the value of zval, and our garbage collection mechanism is also based on this.

typedef struct _zend_refcounted_h {
    uint32_t         refcount;          /* reference counter 32-bit */
    union {
        struct {
            ZEND_ENDIAN_LOHI_3(
                zend_uchar    type,
                zend_uchar    flags,    /* used for strings & objects */
                uint16_t      gc_info)  /* keeps GC root number (or 0) and color */
        } v;
        uint32_t type_info;
    } u;
} zend_refcounted_h;
Copy after login

All complex type definitions start with the zend_refcounted_h structure. In addition to reference counting, this structure also has GC-related structures. Therefore, when doing GC recycling, the GC does not need to care about the details. What is the type? All of them can be treated as zend_refcounted* structures.

Automatic recycling of variables

In PHP, except for array and object type variables, the rest Most of them are automatically recycled

php. The recycling of ordinary variables is related to the number of references to the variable.

Official example

$a = 1;
$b = $a;
xdebug_debug_zval('a');
$a =10;
xdebug_debug_zval('a');
unset($a);
xdebug_debug_zval('a');
Copy after login

Result

a:
(refcount=2, is_ref=0),int 1
a:
(refcount=1, is_ref=0),int 10
a: no such symbol
Copy after login

You can see that when $a =10, the COW (copy-on-write) mechanism of php is involved, $b A copy of the original $a will be copied, and the reference relationship between them will be released, so the number of references (refcount) of a is reduced to 1.

Then the number of references to a becomes 0 after we uset($a). This will be considered a garbage variable and free up space.

Give an example

$a = [1];
$a[1] = &$a;
unset($a);
Copy after login

The type of $a before unset($a) is a reference type

a:
(refcount=2, is_ref=1),
array (size=2)
  0 => (refcount=1, is_ref=0),int 1
  1 => (refcount=2, is_ref=1),
    &array<
Copy after login

Detailed explanation of php7 garbage collection mechanism

unset($ a) After that, it becomes like this

Detailed explanation of php7 garbage collection mechanism

At this time, when we unset the refcount changes from 2 to 1, because there is an internal reference pointing to $a, so where it is externally The occupied space will not be destroyed.

Then our external reference has been interrupted and we cannot use it. It becomes an "orphan", which is called a wild pointer in the C language. In php it's called a circular reference. Memory leak. If you want to destroy the variable, you can only wait for the php script to end.

Memory leaks caused by circular references

In order to clean up these garbage, two criteria are introduced

● If the reference count is reduced to zero, the variable where it is located The container will be cleared (free) and is not garbage

● If the reference count of a zval is still greater than 0 after being reduced, then it will enter the garbage cycle. Secondly, during a garbage cycle, find out which parts are garbage by checking whether the reference count is reduced by 1 and checking which variable containers have zero references.

Circular references basically only appear in arrays and objects. The object is because it itself is a reference.

The recycling process of object and array

php7's garbage collection consists of two parts, one is the garbage collector and the other is the garbage collection algorithm.

The garbage collector collects the elements just mentioned that may be garbage into the recycling pool, that is, the zend_refcount information of the variable is placed in the recycling pool. When the value of the recycling pool reaches a certain amount, it will be processed uniformly.

The processing process is relatively simple.

Traverse every variable in the recycling pool, and then traverse each member based on each variable. If the members are still nested, continue traversing. Then set the simulated refcount of all members to -1. If the reference count of the external variable is 0 at this time. Then it can be considered garbage, clearly. If it is greater than 0, then the number of references is restored and taken out from the garbage collection pool.

The principle of garbage collection

If your variable is not garbage, then after the references of all its member variables are reduced by one, the reference of the total variable will definitely not be 0.

Example

It’s rather hard to say, so why not give an example. When I first browsed sf.gg, I saw a question about GC, and I answered it. About the GC garbage collection mechanism

The topic is as follows

Detailed explanation of php7 garbage collection mechanism

//我的回答
1、只要zval.value的refcount减一,然后缺其refcount的值不为0那么它就可能是垃圾,进入垃圾周期。
2、进入垃圾池遍历所有成员,包括其嵌套的成员,都对其做 refcount-1的操作,看外部的引用是否为0。
那么对于 题主的问题来说,
首先,你要想$a为垃圾,一定要先对 unset($a)操作,那么此时 $a的 refcount = 2
对于$a[0] refcount-1 不影响外部的$a,
$a[1] refcount-1 ,此时 $a的 refount=1
$a[2] refcount-1 ,此时 $a 的 refount=0 
模拟减结束,那么此变量被当成垃圾回收。
Copy after login

The above is the detailed content of Detailed explanation of php7 garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template