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 is the principle of gc in golang

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about the principle of gc in golang. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Gc appears

For anyone who uses C, if you ask them what is the biggest annoyance of C, many of them may answer that pointers and memory leaks. As a result, the later languages are helping programmers deal with memory leaks. The more famous languages java, python, go, and so on, all have a more important mechanism, that is, gc (Garbage Collection), that is, garbage collector. Of course, this is also proud of the characteristics of these languages, they all have a mechanism such as runtime in golang, which is managed by jvm in java. Programmers no longer need to think about when I should allocate memory and when I should reclaim it. If you are learning programming from the c language, you should be very familiar with malloc and free, and then use these two functions carefully when writing code. If you study python, you won't notice this problem at all, and you won't care about it. It is worth mentioning that using char lists to store strings in C language is a very troublesome thing, in java and so on, but even so, programmers should pay attention to gc problems in the daily process of writing programs. A relatively poor design may make the burden of gc on the whole program very large, and you may find that the proportion of gc time in the program is very high.

The principle of gc

The common ways of gc are:

Reference count (reference counting) each object maintains a reference counter. When the object referencing the object is destroyed or updated, the reference counter of the referenced object is automatically minus 1. When the applied object is created or assigned to another object, the reference is + 1, and the reference is recycled when the reference is 0. The idea is simple, but updating the reference counter frequently degrades performance, and there is a loop to reference (used by php,Python).

Tag cleanup (mark and sweep) is used by golang, traversing all referenced objects from the root variable, clearing the tag after the operation, and collecting unmarked objects. Disadvantages: every garbage collection will suspend all the normal running code, the response ability of the system will be greatly reduced, a variety of mark&swamp variants (tricolor marking method), alleviate performance problems.

Generation collection (generation) jvm on the use of generation recycling ideas. In object-oriented programming languages, the life cycle of most objects is very short. The basic idea of generational collection is to divide the heap into two or more spaces called generation. Newly created objects are stored in what is called the young generation (generally speaking, the new generation will be much smaller than the old), and as garbage collection is repeated, objects with a longer life cycle will be promoted (promotion) to the old age (a classification idea is used here, which is also a basic idea of scientific thinking).

Gc principle in golang

Before go1.3, the biggest problem with gc is stw (stop the word), that is, when you gc, you need to pause the program behavior, then enter the tag, and finally remove the unmarked garbage. If gc is triggered frequently, the program will run on one card. The basic idea is:

1. Tag: in the memory heap (called heap memory because the heap data structure is sometimes used to manage memory pages) stores a series of objects that may be associated with other objects (references between these objects) a tracing garbage collector stops the running program at some point in time, and then it scans the object collection (already known set of objects) that runtim e already knows Usually they are global variables and various objects that exist in stack. Gc will mark these objects, mark the status of these objects as reachable, find out all of them, from the current these objects can reach the reference of objects elsewhere, and mark these objects as reachable objects, this step is called mark phase, that is, the marking phase, the main purpose of this step is to obtain the state information of these objects.

two。 Recycling: once all these objects are scanned, gc will get all the objects that cannot be reach (objects with a state of unreachable) and recycle them. This step is called sweep phase, that is, the cleaning phase.

3. Clear: gc collects only those objects that are not marked as reachable. If gc does not recognize a reference, it may eventually recycle an object that is still in use, resulting in a program running error.

Go introduced concurrency cleanup in 1. 3, and go team itself said that it reduced the pause time by 50% and 70%.

Go used tricolor notation at 1.5, which is an upgraded variant of the tag removal algorithm. The process is as follows:

1. Gray: the object is marked, but the object contains sub-objects that are not marked

two。 Black: the object has been marked, and the child objects contained in this object have also been marked, and the bit corresponding to gcmarkBits is 1 (this object will not be cleaned up in this GC)

3. White: the object is not marked, and the bit corresponding to gcmarkBits is 0 (the object will be cleaned up in this GC)

For example, if there are six objects in the current memory, the root object aforce b is a local variable assigned on the stack, the root objects an and b refer to objects An and B respectively, and the B object refers to object D, then the state of each object before GC starts is shown in the following figure:

1. All objects are white in the initial state.

two。 Then start scanning the root objects an and b; because the root objects refer to objects An and B, then An and B become gray objects, and then begin to analyze the grey objects. when analyzing A, A does not reference other objects and quickly turns to black, while B refers to D, then B needs to turn D to gray for the following analysis.

3. The gray object has only D, and since D does not refer to other objects, D turns to black. End of marking process

4. Eventually, the black object will be retained and the white object will be recycled.

Gc process in go

GO's GC is parallel GC, that is, most of the processing of GC runs at the same time as ordinary go code, which makes GO's GC process more complex.

1.Stack scan:Collect pointers from globals and goroutine stacks . Collect the root object (global variables, and G stack) and turn on the write barrier. Global variables, turn on the write barrier requires that STW,G stack only need to stop the G, less time.

2.Mark: Mark objects and follow pointers . Mark all root objects, and all objects that can be reached by the root object are not recycled.

3.Mark Termination: Rescan globals/changed stack, finish mark . Rescan the global variables and the stack (write barrier) of the previous round of changes to complete the marking work. This process requires STW.

4.Sweep: sweep span by marking result

Golang after 1.8canceled the first step of stop the world, which is another optimization. Since 1.9, the implementation of write barrier uses Hybrid Write Barrier, which greatly reduces the time of the second STW.

Because go supports parallel GC, GC scanning and go code can run at the same time, which brings the problem that go code may change the dependency tree of objects during GC scanning.

For example, root objects An and B are found at the beginning of the scan, and B has a pointer to C.

1.GC scan An and put it in black first.

2.B give the pointer of C to A.

3.GC scanned BMIT B and put it in black.

4. If C is white, it will be recycled; but An actually refers to C.

To avoid this problem, go enables the write barrier (Write Barrier) during the marking phase of GC. After the write barrier (Write Barrier) is enabled, C is grayed out according to the write barrier mark in the third round rescan phase of GC to prevent C loss.

These are all the contents of the article "what is the principle of gc in golang?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Database

Wechat

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

12
Report