Let's talk about the java garbage collection mechanism and java garbage collection mechanism.
I have read some articles about the garbage collection mechanism recently.
To return to java's garbage collection mechanism, we should answer the following three questions:
1. What memory will be reclaimed?
2. When will it be withdrawn?
3. How to reclaim it?
First, let's talk about which objects will be withdrawn.
The root search algorithm (GC Roots Tracing) is used to determine whether the object is still alive. This algorithm uses a series of objects named "GC Roots" as the starting point and uses these nodes to perform a downward search, when no reference link is provided between an object and GC Roots (in graph theory, it is impossible to reach this object from GC Roots), it indicates that the object is unavailable, they are determined to be recycled objects.
Second, when to recycle the memory.
When the cpu is idle or the memory space is insufficient, garbage collection is performed, and programmers cannot control when garbage collection is performed in this province.
Finally, how to recycle garbage.
The Java language specification does not clearly indicate which garbage collection algorithm is used by JVM. Most garbage collection algorithms use the root set concept; the so-called root set refers to the collection of referenced variables (including local variables, parameters, and class variables) that can be accessed by Java programs that are being executed ), the program can use reference variables to access object attributes and call object methods. The first choice of garbage collection is to determine which are reachable and which are inaccessible from the root, and the objects reachable from the root set are all active objects, which cannot be recycled as garbage, this also includes objects indirectly accessible from the root set. Objects that cannot be reached through any path in the root SET meet the garbage collection conditions and should be recycled. There are roughly the following methods:
1. Reference Counting Collector)
The reference counting method is the only method that does not use the root set for garbage collection. This algorithm uses the reference counter to distinguish between a surviving object and an object that is no longer in use. Generally, each object in the heap corresponds to a reference counter. When an object is created and assigned to a variable, the reference counter is set to 1. When an object is assigned to any variable, the reference counter is added with 1 each time. When the object is out of scope (this object is not used anymore), the reference counter is reduced by 1. Once the reference counter is 0, the object meets the garbage collection conditions.
2. tracing Algorithm (TracingCollector)
The tracing algorithm is proposed to solve the problem of reference counting. It uses the concept of root set. The Garbage Collector Based on the tracing algorithm scans the root set to identify which objects are reachable and which objects are not reachable, and marks the reachable objects in some way, for example, you can set one or more places for each reachable object. In the scanning and identification process, the garbage collection based on the tracing algorithm is also called the mark-and-sweep garbage collector.
3. compacting algorithm (CompactingCollector)
To solve the heap fragmentation problem, tracing-based garbage collection absorbs the Compacting algorithm IDEA. In the process of clearing, the algorithm moves all objects to the end of the heap, the other end of the heap becomes an adjacent idle memory zone. The Collector updates all references of all objects it moves, so that these references can recognize the original objects at the new location.
4. CopingCollector)
This algorithm is proposed to overcome the handle overhead and solve the garbage collection of heap fragments. At the beginning, it divides the heap into one object plane and multiple idle planes. The program allocates space for the object from the object plane. When the object is full, garbage Collection Based on the coping algorithm scans activity objects from the root set and copies each activity object to the idle surface (so that there is no idle hole between the memory occupied by the activity object ), in this way, the idle surface becomes the object surface, and the original object surface becomes the idle surface. The program will allocate memory in the new object surface.
A typical garbage collection Algorithm Based on the coping algorithm is the stop-and-copy algorithm, which divides the heap into the object plane and the free area plane. During the switching process between the object plane and the free area, the program is suspended.
5. GenerationalCollector)
One defect of the stop-and-copy garbage collector is that the collector must copy all the active objects, which increases the program wait time, which is the cause of the inefficiency of the coping algorithm. In program design, there is a rule that most objects have a short time and a few objects have a long time. Therefore, the generation algorithm divides the heap into two or more sub-heaps as the generation (generation) of objects ). Because most objects have a short time, as the program discards unused objects, the garbage collector collects these objects from the youngest child heap. After the generational garbage collector is run, the objects that survived the last run are moved to the subheap of the next highest generation. Because the subheap of the old generation is not often recycled, this saves time.
There are many garbage collection algorithms, which reference counter algorithms, tag clearing algorithms, replication algorithms, and tag sorting algorithms. With the development, many algorithms are used currently. It is divided into the new generation and the old generation, which is suitable for frequent creation and high mortality. In the old age, the survival rate is high. For the memory allocation method, see.
The ratio of the new generation to the old generation is. Objects Created by a large number of rows are created on Eden space, but large objects are directly created in the old age to avoid frequent movement of large objects. A large number of newly created objects will soon die, that is, when the memory space on the Eden is insufficient, minor gc is performed to recycle the memory on Eden space and from space, and the replication algorithm is used to copy live, reachable objects to space, at this time, all objects 'age' + 1 will be copied to the old generation when a certain 'age' is reached, and the old generation is also a new generation replication algorithm, the problem of insufficient memory space may be guaranteed. At this time, the next step of to sapce is to from space, which keeps repeating. In the old age, when the memory space is insufficient, full gc is executed and the tag-sorting algorithm is used.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.