Atitit. Principle and Design of Time base gc garbage collection, atitit. timegc
Atitit. Principles and Design of Time base gc garbage collection
1. MRC (MannulReference Counting manual retain/release/autorelease Statement 1
2. Automatic garbage collection (GC) 1
3. Arc ARC is between automatic garbage collection (GC) and manual memory management. 1
3.1. The most basic running methods of various garbage collection algorithms can be divided into three types: 1. reference counting: 1
3.2. mark-sweep 2
3.3. Copy a node ). Generation 2
3.4. Attilax time base gc3
4. How does GC work? 3
5. How to associate ---- wrap file, network socket4
6. Refer to 4
1. MRC (MannulReference Counting manual retain/release/autorelease statement
2. Automatic garbage collection (GC)
3. Arc ARC is between automatic garbage collection (GC) and manual memory management.
ARC is between automatic garbage collection (GC) and manual memory management. Like garbage collection, ARC eliminates the need for programmers to write retain/release/autorelease statements. However, unlike garbage collection, ARC cannot process retaincycles. In ARC, if two objects strongly reference each other (strong references), they will never be released, or even no object references them. Therefore, although ARC can avoid most of the programmer's memory management problems, it still requires the programmer to avoid retaincycles or manually interrupt the retain loop between objects.
Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui) Name: AI long, EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://www.cnblogs.com/attilax/
3.1. The most basic running methods of various garbage collection algorithms can be divided into three types:
1. reference counting ):
The basic idea is to add a counter for each object and record the number of references pointing to this object. Each time a new reference points to this object, the counter increments by one; otherwise, each time a reference pointing to this object is set to null or pointing to another object, the counter decreases by one. When the counter changes to 0, this object is automatically deleted.
The advantage of reference counting is that 1) it is relatively simple and does not require much runtime support. It can be implemented in languages that do not support GC in the native. 2) the object will be released at the moment it becomes garbage and will not cause additional interruptions to the execution of Normal programs. Its dead point is circular reference. Object A contains A reference pointing to object B, and object B contains A reference pointing to object A, so the counter will be blind. In addition, the reference count has an impact on the execution performance of Normal programs (the counter must be changed every time the reference value is assigned), especially in the multi-threaded environment (the counter must be locked for synchronization ).
Examples of reference counting are Apple's ARC, std: shared_ptr in the C ++ standard.
3.2. mark-sweep)
.
The basic idea is to allocate resources on demand first. When there is no idle memory, we start from the reference on the register and program stack and traverse the graph composed of objects as nodes and reference as edges, mark all accessible objects, clean up the memory space, and release all unmarked objects.
Mark-cleaning does not solve the issue of loop reference, and does not affect the execution performance of normal Programs If GC is not triggered. However, the problem is that when the memory is used up to trigger GC, the normal program needs to be interrupted for a period of time to clean the memory. When there are many memory objects, this interruption may be very long.
There are many examples of using or partially using Mark-cleaning, which are not listed one by one.
3.3. Copy a node ). Generation Division
The basic idea is to split the entire memory space into two parts, which may be counted as A and B. All objects are allocated in A. When A is full, it also traverses the graph composed of objects as nodes and reference as edges starting from the reference on the register and program stack, copy all accessible objects to B, and then call roles A and B.
Compared with Mark-cleaning, The main disadvantage of node replication is that half of the space is idle and cannot be used, another obscure disadvantage is that the memory usage method has potential conflicts with the existing memory paging and Cache swap-in and swap-out mechanisms. However, it has a major advantage: all objects are always closely arranged in the memory, so the task of allocating memory becomes extremely simple, just move a pointer. For environments with frequent memory allocations, the performance advantage is considerable. In addition, because the whole memory space does not need to be cleaned up, if there are few surviving objects in the memory and many spam objects (some languages have this tendency ), GC-triggered interruptions may be less than Mark-cleaning.
According to a statistical conclusion, if a memory object is not spam during a Mark process, it is unlikely to become spam in the short term. Generational collection gathers memory objects that are not marked as spam objects during multiple garbage collection processes into another area-the older area, that is, the memory objects in this region are relatively old. Because the probability that memory objects in the elderly area become garbage in the short term is very low, the garbage collection frequency in these areas can be reduced. In contrast, the garbage collection frequency of objects in the young area is high. This improves the overall performance of garbage collection.
=== Dividing line after basic algorithm introduction ===
The above three basic algorithms have their own advantages and disadvantages, and there are also many improvement solutions. Currently, the most successful solution in engineering practice should calculate the generational garbage collection. The basic idea is as follows: a program contains a large number of temporary objects, which will be released soon after being allocated, at the same time, if an object is not recycled for a long period of time after it is allocated, it is very likely that it has a long life cycle and it will be useless to try to collect it. Therefore, you can consciously divide the memory into several blocks based on the "Object age". It may be recorded as "XD". All the allocations are carried out in the Qing Dynasty. The Qing Dynasty is full of GC for the Qing Dynasty, then, move the surviving objects to the middle generation until the middle Qing Dynasty is full, and then move the surviving objects to the old generation. This is just an example of the idea, in practice, there are a wide variety of methods for the generational garbage collection algorithm, and more than one basic algorithm is often used at the same time (such as green proxy node replication and old proxy mark cleaning ).
3.4. Attilax time base gc
4. How does GC work?
The GC workflow consists of the following steps:
1. Mark)
2. Plan)
3. Clean (Sweep)
4. Relocate)
5. Compact)
5. How to associate ---- wrap file, network socket
I want to improve this point. That is to say, all gc-related datasets are combined, and the entire collection process will not touch the memory of user data blocks except for the memory that is no longer used.
The most important thing about gc is to associate the data on the stack. When collection occurs, all temporarily allocated memory blocks on the stack should not be released. C language itself does not provide the features of stack traversal, so it is necessary to have a natural solution that allows users to easily achieve this.
On the user's call stack, at each call level, the temporarily allocated memory is naturally attached to the current stack mount point. Once the call is returned, all temporary memory blocks at the current level should be disconnected from the root. Of course, if the memory block is returned, it must be retained. In C, we need to monitor the entry and exit of each function to ensure correct gc work. (If it is C ++, it is a little more convenient to set a guard object at the function entry point.) because this monitoring process is very frequent, the optimization of it is a key task.
6. Reference
Which garbage collection algorithms are used for implementation in various programming languages? What are the advantages and disadvantages of these algorithms? -GC garbage collection (computer science)-zhiyun.html
Garbage collection mechanism GC knowledge further summary and talk about how to make good use of GC-JeffWong-bokyue.html () imp
Three basic methods for garbage collection (GC)-blog-bole.html