Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does gc mean in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what does gc refer to in php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what does gc mean in php"?

In php, the full name of gc is "Garbage Collection", which means "garbage collection" in Chinese. It is a dynamic memory management mechanism that automatically releases allocated blocks of memory that the program no longer needs. The GC mechanism allows programmers to devote more energy to business logic without having to worry too much about program memory allocation.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, the full name of gc is "Garbage Collection", which means "garbage collection" in Chinese. It is a dynamic memory management mechanism.

The garbage collection mechanism (GC) automatically releases allocated blocks of memory that are no longer needed by the program. The process of automatically reclaiming memory is called garbage collection.

Garbage collection mechanism (GC) allows programmers to devote more energy to business logic without having to worry too much about program memory allocation.

In today's popular languages, garbage collection mechanism is a common feature of the new generation of languages.

The generation of garbage

In the data structures of complex types in PHP7, such as strings, arrays, objects, etc., there is a gc in the header, and the purpose of this gc is to support garbage collection. When the variable is assigned and passed, the reference number of value will be increased, and when unset, return and other variables are released, the reference number will be subtracted, and if it is found that refcount becomes 0, value will be released directly, which is the basic process of variable recovery.

However, there is one problem that cannot be solved by this mechanism, which is the problem of circular reference.

What is a circular reference? To put it simply, the value stored inside the variable refers to the variable itself. This comparison often occurs with variables of array and object types.

Let's start with the reference, the zend_reference type, which is a new variable type added by PHP7. When you use the "&" operation on the variable, a new intermediate structure, zend_reference, is created, which really points to the corresponding value structure.

For example:

/ / when assigning values as follows: $a = 'hello'; / / $a-> zend_string$b = $a; / / $b zend_string$c = & $b; / / $c type = IS_REFERENCE, refcount = 2)-> zend_string

Eventually it will look like this:

That is, the zval of $b and $c is pointed to the final zend_string through the intermediate structure zend_reference.

Going back to the problem of circular references, give an example of array circular references:

$a = [1]; $a [] = & $a share unset ($a)

After using the & operation, the variable a becomes a reference type with a reference count refcount of 2, and assigns values to its own elements, that is, variable a becomes a reference to itself.

The details are as follows:

When you unset, it looks like this:

That is, the zval type in which $an is located has become IS_UNDEF, and the reference count of the zend_reference structure is minus 1, but it is still greater than 0. at this time, this part of the structure becomes garbage, and if it is not dealt with, it may cause a memory leak. Here you need the garbage collector to collect this part into the buffer and then recycle it.

Recovery process

If the refcount of the variable is greater than 0 refcount, PHP will not immediately identify and collect the garbage of the variable, but will put it into a buffer and deal with it uniformly when the buffer is full (10000 values). The gc in the variable zend_value will be added to the buffer. Currently, garbage will only appear in the array and object types. The situation of the array has been described above. The case of an object is caused by the member property referencing the object itself, and other types do not have members of this variable referencing the variable itself, so garbage collection will only handle these two types of variables.

The structure of gc zend_refcounted_h is as follows:

Typedef struct _ zend_refcounted_h {uint32_t refcount / / record the number of references to zend_value union {struct {zend_uchar type, / / zend_value type, consistent with zval.u1.type zend_uchar flags, uint16_t gc_info / / GC information, record the location and color in the gc pool, garbage collection process will use} v; uint32_t type_info;} u } zend_refcounted_h

A variable can only be added to the buffer once. In order to prevent repeated addition, the zend_refcounted_h.gc_info will be set to GC_PURPLE, that is, marked purple, and will not be repeatedly inserted later.

Garbage buffer is a two-way linked list, wait until the cache is full, then start the garbage check process: traverse the buffer, traverse all the members of the current variable, and then subtract the member's refcount by 1 (recursive traversal is also performed if the member also contains child members, that is, depth-first traversal), and finally check the reference of the current variable, which is garbage if reduced to 0. The core principle of this algorithm is: garbage is caused by members referencing themselves, so subtract references from all members. If you find that the refcount of the variable itself becomes 0, it means that all its references come from its own members, that is, it is no longer used anywhere else, then it is garbage and needs to be recycled. On the contrary, it means that it is not garbage and needs to be removed from the buffer. The specific process is as follows:

(1) start traversing the roots of the buffer linked list, mark the current value as gray (zend_refcounted_h.gc_info is set to GC_GREY), and then traverse the members of the current value by depth-first, subtract the refcount of the member value by 1, and mark it as gray.

(2) repeatedly traverse the buffer linked list to check whether the current value reference is 0, and if 0 means it is indeed garbage, mark it as white (GC_WHITE). If it is not 0, it rules out the possibility that all references come from its own members, indicating that there are external references, not garbage. At this time, because the member is refcount minus 1 in step (1), it needs to be restored again. Do a deep traversal of all members, add 1 to the member refcount, and mark it as black

(3) traverse the buffer linked list again, remove the non-GC_WHITE nodes from the roots linked list, and finally all the real garbage in the roots linked list, and finally remove the garbage.

At this point, I believe you have a deeper understanding of "what gc refers to in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report