Article Directory
- What is garbage collection
- Common garbage collection (GC) algorithm
- Reference count (reference counting)
- Mark - Clear (mark and sweep)
- Three color labeling
- Node Copy (Copying Garbage Collection)
- Generational collection (Generational Garbage Collection)
- Golang garbage collection
- reference
What is garbage collection
Once upon a time, memory management is a major problem programmers to develop applications. The traditional system-level programming languages (mainly C / C ++), one must be careful of memory management operations, control application and release memory. The slightest mistake, it is possible to generate a memory leak problems that difficult to find and difficult to locate, has been a nightmare plagued developers. How to solve this headache problem? Over the past generally use two methods:
- Memory leak detection tools. The principle of this tool is generally static code scanning, code segment of memory leaks can occur through the scanner detects. However, detection tools will inevitably be omissions and deficiencies, can only play a supporting role.
- Smart pointers. This is an automatic memory management method introduced in c ++, the object referenced by the object pointer with automatic memory management functions, a programmer need not be too concerned about the release of memory, and memory to achieve the purpose of automatic release. This method is the most widely adopted practice, but programmers have some learning costs (not the native language support level), and once there is forget to use scenes still can not avoid memory leaks.
To solve this problem, and later developed almost all the new languages (java, python, php, etc.) have introduced the language level of automatic memory management - that is, language users apply only concern with memory without having to be concerned about the release of memory memory is released automatically manage or runtime (runtime) by the virtual machine (virtual machine). And this memory resources are no longer used for automatic recovery of behavior is known as garbage collection.
Common garbage collection (GC) algorithm
Common GC algorithm are:
- Reference count (reference counting)
- Clear labeling (mark & sweep)
- Three color labeling
- Node Copy (Copying Garbage Collection)
- Generational collection (Generational Garbage Collection)
Reference count (reference counting)
This is the simplest kind of garbage collection algorithms, smart pointers and similar to previously mentioned. Maintaining a reference count for each object, the referenced object reference to the object when the object is destroyed or updating a reference count decremented, the reference count when the assignment to other objects referenced objects are created or incremented by one. When the reference count is zero immediately be recovered. The reference count is gradual, memory management overhead can be distributed into the entire program. This algorithm is more intense in memory and real-time high of more widely used in the system. Currently reference counting method is mainly used in c ++ standard library's std :: shared_ptr, Microsoft's COM, Objective-C and PHP.
advantage:
- Progressive. Memory management and user program execution intertwined, dispersed GC price to the entire program. Unlike the mark - sweep algorithm requires STW (Stop The World, GC when the user suspends program). But the business code and algorithms coupled with GC, GC result in deterioration in business performance code execution, variable points to the more frequent changes, the higher the GC occupied properties.
- Algorithm is relatively simple
- The memory unit can be quickly recovered. Compared to other garbage collection algorithms, heap is exhausted or reaches a certain threshold will be garbage collected.
Disadvantages:
- Circular reference problem. When the circular reference occurs between object references in the chain of objects can not be released. The most obvious solution is to avoid circular references (such as strong references, etc.), such as the introduction of strong cocoa weak pointer and a pointer two kinds of pointer type. Or the system reference cycle detection and active chain to break the cycle. Of course, this also increases the complexity of garbage collection.
- Frequently updated reference count reduces the performance and operating efficiency. Update the memory unit and so need to delete the reference count maintenance associated memory units, compared to some of the track-type garbage collection algorithm does not require these costs. A simple solution is to compiler adjacent reference count update incorporated into the next update; there is a way for the temporary variable frequent references will not be counted, but verify by scanning stacks in the reference reaches 0 there are temporary object references and decide whether to release.
Mark - Clear (mark and sweep)
Mark - sweep algorithm is the first automatic memory management and garbage collection algorithms based on tracking. Algorithms idea put forward in the 1970s, is a very old algorithm. Memory cell is not immediately recovered and trashed, but remains unreachable state until it reaches a certain threshold or fixed length of time. This time the system will suspend a user program, that is, STW (Stop The World), turn the garbage collection program. Garbage collection program for all units to conduct a global survival traversed to determine which unit can be recycled. Algorithm is divided into two parts: the marks (Mark) and the cleaning (sweep). Mark phase indicates that all surviving units will sweep phase garbage unit.
Mark - sweep algorithm advantage is garbage collection algorithm has the advantage that tracking based on: counting algorithm avoids the disadvantages of the reference (circular references can not handle, it is necessary to maintain a pointer). But the algorithm also has a flaw, it is often said that the issue of STW (Stop The World). Because the algorithm must suspend the entire program in the mark, or other threaded code may change the state of the object, which should not be possible to recover objects as garbage collected.
When the objects in the program gradually increased, recursively traverse the entire object tree will consume a lot of time, this time in large programs could be millisecond level. So that all users wait for a few hundred milliseconds GC time it can not be tolerated. golang 1.5 previously used this algorithm.
Of course, follow-up, there have been many variants mark & sweep algorithms (such as three-color notation) optimization problem.
Three color labeling
Is a conventional three-color labeling marker - clear (mark and sweep) the mark phase improvement, it is a concurrent GC algorithm.
Principle is as follows:
- First create three collections: white, gray, black.
- All objects in white collection.
- Then traversing all objects from the root node (note that this is not recursive traversal), the objects traverse from white to gray collection into the collection.
- After traversing a collection of gray, gray object referenced objects from a collection of white into gray collection, after this gray objects into black collection
- 4 is repeated until no objects in gray
- By write barrier change (Write Barrier) be detected, the above operation is repeated
- Collect all the white objects (garbage)
This algorithm can be implemented "on-the-fly", while the program is executed concurrently collected (mark phase), do not need to suspend the entire program (later speaks specifically how GC and business code concurrent execution, in fact, there will still be short of the STW).
But there will be a flaw, three color labeling incremental GC algorithm, the program may speed the waste generated will be greater than the speed of garbage collection, this will lead to more and more programs in the garbage can not be collected.
Using this algorithm is Go 1.5, Go 1.6.
Node Copy (Copying Garbage Collection)
Node copy is tracking algorithm. The entire stack which is equally divided into two half-regions (semi-space), comprising a prior data, the other contains obsolete data. Node copy garbage collection starts from two half region of the switching roles (Flip), then collected in the old half-area, i.e. Fromspace survival data structure traversal, the first access unit to copy it to a the new half-area, which is Tospace go. After Fromspace all surviving units have been visited, a copy of the data collector establishes a structure to survive in Tospace, the user program can resume running.
advantage:
- All surviving data structure are reduced and arranged in the bottom Tospace, so there is no problem of memory fragmentation.
- Get new memory can be achieved simply by incrementing the free-space pointer.
Disadvantages:
- The memory is not fully utilized, there is always half of the memory space is wasted state.
Generational collection (Generational Garbage Collection)
A major problem is a waste of time on the long life cycle of objects (objects long life cycle does not require frequent scanning) - garbage collection algorithm to track (cleaning, node copy tag) based on At the same time, there is such a memory allocation fact that "most object die young". So after a lot of real observe that, in object-oriented programming language, the vast majority of the object life cycle is very short, so the length of the life cycle of the object to be generational. Generational collection algorithm is a conventional Mark-Sweep improvement of the life cycle of an object according to the length of the store heap to two (or more) regions, which is generational (generation).
The newly created object is stored in is called the Cenozoic (young generation) in (generally speaking, the size of the new generation will be much smaller than the old one's), with the repetition of the implementation of garbage collection, long life cycle objects will be promoted ( promotion) to the old era. For the new generation of regional garbage collection frequency was significantly higher than the region's old. Therefore, the new generation garbage collection and garbage recall of two different old age garbage collection came into being, respectively, for the garbage collection on their space objects. The new generation garbage collection is very fast, faster than the old year several orders of magnitude, even higher frequency new generation garbage collection, the efficiency is still stronger than the old one's garbage collection, because the life cycle of most objects are short-lived , no need to upgrade to the old era.
Principle is as follows:
- The new objects in generation 0
- When a small memory amount exceeds the threshold, triggering the collection of generation 0
- The surviving generation 0 objects (uncollected) into the first generation
- Only when more than one high memory usage threshold to trigger the generation collection
- Similarly the second generation
because generation 0 objects is very small, so each time you collect will traverse very fast (collect orders of magnitude faster than a generation). Only when memory consumption is too large to trigger a slower 1 and 2 generation collection.
Therefore, generational collection is a better way of garbage collection. The language used (platform) has jvm, .NET. GC points are generally three generations, in java called New Generation (Young Generation), the old generation (Tenured Generation) and permanent substituting (Permanent Generation); .NET called generations 0, the first and second generation of 2 generations.
Golang garbage collection
golang history of the evolution of GC
- 1.3 the previous version, golang garbage collection algorithm is relatively simple traditional Mark-Sweep algorithm, its performance is also widely criticized: go runtime under certain conditions (memory exceeds the threshold value or periodically, such as 2min), to suspend execution of all tasks performed mark & sweep operation, the operation is completed to start execution of all tasks. In more memory usage scenarios, go program very obvious Caton phenomenon (Stop The World) occur during garbage collection. In response speed requirements higher background service process, this delay is simply intolerable! Many domestic and foreign production team go practice the language more or less stepped gc pit this time. It was more common solution to this problem is to control the amount of memory assigned automatically as soon as possible to reduce the memory load gc, while the manual method requires a lot of memory management processing and frequency allocated memory scene.
- Version 1.3, go team began gc performance for continuous improvement and optimization, mainly to Sweep changed to parallel operation. Each new version of the go gc improve when we publish have become key points of concern. Version 1.3, go runtime separating the mark and sweep operations, as before, is to suspend the implementation of all tasks and start the mark, the mark is completed immediately restart the suspended task, but to sweep task and coroutine common tasks Like parallel with the execution of other tasks. If you are running on multicore processors, go gc task will attempt to put on a separate core running and trying not to affect the implementation of the business code. go team's own words is reduced by 50% -70% of the time suspended.
- Version 1.4 is not much change on the performance of gc. Version 1.4 runtime lot of code to replace the native language and the use of the c language go on gc brought a big change can be precise gc. c language can not obtain the object information in memory when gc, and therefore can not accurately distinguish between ordinary variables and pointers, only the pointer as ordinary variables, if this happens to ordinary space variable points of other objects, and that the object will not be recycling. And go language is fully aware of the type of information object, the mark will traverse the object pointer, thus avoiding the waste heap memory (solve about 10-30%) at the C implementation.
- Version 1.5 of gc go team has conducted a relatively large improvements (1.4 already foreshadowed such as the introduction of write barrier), the official main goal is to reduce latency. go 1.5 is implementing the garbage collector is " presumptuous generation, non-moving, complicated, three-color marker garbage collector ." Generational algorithm already mentioned, is a better garbage collection management policy, and then version 1.5 does not consider realization; I guess the reason is that the pace can not be too big a step, have gradually improved, go the official said that it would in gc optimized 1.6 version is taken into account. While introducing the three color notation introduced above, mark operation of this method is that every time without progressive scanning can be performed throughout the memory space, you can reduce the time to stop the world.
- From the first step of 1.8 golang stop the world also canceled, this is an optimization.
- 1.9 began to realize write barrier uses Hybrid Write Barrier, significantly reducing the time of the second STW.
golang 的 GC
root
First mark root root object, the child object is the root object of survival.
Root object comprising: a global variable, each of the variables G stack.
mark
span go is the smallest unit of memory management.
bitmap
As shown, if the block flag span by gcmarkBits bitmap is referenced. Memory allocation bitmap corresponding to the region.
Three Color Marker
- Gray: The object has been marked, but the child objects of this object contains unlabeled
- Black: object has been marked, the object and the child object also contains numerals, gcmarkBits corresponding bit is 1 (the object will not be cleaned in this GC)
- White: the object is not marked, gcmarkBits corresponding bit is 0 (the subject will be cleaned in this GC)
For example, the current memory have A ~ F total of six objects, the root object a, b themselves as local variables allocated on the stack, the root object a, b respectively refer to objects of A, B, and B is the object and reference object D, the GC state before the start of each object as shown below:
- All objects in the initial state are white.
- Then start scanning the root object a, b; root object because the object references A, B, then A, B becomes gray objects, then began Gray object analysis A, A does not refer to other objects quickly into black, B cited D, while B is also need to turn black into gray D, conducted the following analysis.
- Gray D objects only, because the D does not refer to other objects, so D into the black. The labeling process ends
- Eventually, the black object will be preserved, a white object will be recovered out.
STW
stop the world's biggest performance problems gc, gc for, need to stop all memory changes, stop all goroutine, wait until after the restoration gc.
Go trigger garbage collection
- Threshold: The default memory doubled to start the gc
- Regular: 2min trigger a default gc, src / runtime / proc.go: forcegcperiod
- Manual: runtime.gc ()
GC process
GO The GC is parallel GC, GC treatment is most common go and run code, which makes the process more complicated GO of GC.
- Stack scan: Collect pointers from globals and goroutine stacks. Gather root object (global variables, and G stack), open the write barrier. Global variables need to open the write barrier STW, G stack need only stop the G like, less time.
- Mark: Mark objects and follow pointers. Mark all root objects, the root object and all the objects that can be reached is recovered.
- Mark Termination: Rescan globals / changed stack, finish mark. Rescan global variables, and the last round of changes in the stack (write barrier), marking the completion of the work. This process requires STW.
- Sweep: sweep span results by tag
Currently the GC process will be carried out twice STW (Stop The World), Stack scan is the first stage, second stage is Mark Termination.
- STW will first prepare the root object scanning, start writing barrier (Write Barrier) and secondary GC (mutator assist).
- The second part of the root STW will be re-scanned object, disable write barrier (Write Barrier) and secondary GC (mutator assist).
From the first step of 1.8 golang stop the world also canceled, this is an optimization; 1.9 began to realize write barrier uses Hybrid Write Barrier, significantly reducing the time of the second STW.
Write barrier
Because go to support parallel GC, GC scans and go code can run at the same time, this problem is caused by the scanning process GC's go change the code is likely to depend on the object tree. Write barrier is to collect the mark phase object dependency tree modify records.
A root object such as that found at the start of the scan and B, B to C has the pointer.
- GC scans A, A into the black
- B A pointer to the C
- GC rescan B, B into black
- C in white, will recover; but in fact quoted C. A
To avoid this problem, go enables write barrier (Write Barrier) during the mark phase of the GC.
When you enable the write barrier (Write Barrier), the GC rescan the third round stage, according to the write into the gray barrier marking the C, C to prevent loss.
note
Go addition to the standard three-color collection, as well as a secondary recovery function to prevent excessive collection of waste generated but the situation come. This part of the code runtime.gcAssistAlloc in.
But golang not generational collection, so for a huge amount of small objects or bitter hand, the mark will cause the entire process is very long, and in some extreme cases, even lead to GC threads occupy more than 50% of the CPU.
Therefore, when the program gc problems caused by large numbers of small objects due to high concurrency, the best you can use sync.Pool such as object pooling technique, to avoid a large number of small objects to increase the GC pressure.
reference
Golang GC garbage collection Detailed
Golang garbage collection
golang garbage collection