Detailed explanation of garbage collection algorithm in JVM (mark removal algorithm, mark copy algorithm, mark sorting algorithm)

Detailed garbage collection algorithm

When it comes to garbage collectors, we must mention the garbage collection algorithm, because all garbage collectors are implemented based on the garbage collection algorithm. The garbage collection algorithm is the methodology of the garbage collector. After understanding these methodology, the work of the garbage collector The principle becomes clear. Speaking of garbage collection algorithms, then we must mention the generational collection theory, because with the generational collection theory, there is a garbage collection algorithm. Click to view the establishment of the generational collection theory. The garbage collection algorithm we use in jvm is introduced below.

  1. Mark-sweep algorithm
    What is the mark-sweep algorithm?
    The earliest and most basic garbage collection algorithm is the "mark-sweep algorithm". The algorithm is divided into two parts: marking and clearing. When reclaiming objects, first mark the objects to be cleared, and clear these marked objects after the marking is completed (you can also mark the survival Objects, clear unmarked objects), the marking process is the process of judging whether the object is garbage (using the reachability analysis algorithm) and most of the subsequent garbage collection algorithms are improved based on the "mark-sweep algorithm" .
    Advantages : The
    most basic and simple garbage collection algorithm lays the foundation for subsequent garbage collection algorithms.
    Disadvantages :
    ①. The execution efficiency is unstable. With the increasing number of objects, the efficiency of marking and clearing of the algorithm will continue to decrease, resulting in unstable execution efficiency. For example, most objects in the new generation are dying. Not suitable for this algorithm. ②. Memory space is fragmented. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much space fragmentation will lead to no suitable space when large objects need to be allocated, so GC will be started early or frequently.
    Application :
    The most commonly used implementation of the "mark-sweep algorithm" is CMS. The collection area of ​​the garbage collector is the old age (the next article will introduce these two garbage collectors in detail) (the next article will introduce this in detail) Garbage collector).
    Insert picture description here

  2. Mark-copy algorithm
    What is the mark-copy algorithm?
    Divide the memory into two pieces of equal size, and use only one of them each time. When the first piece of memory is used up, copy the objects in this piece of memory to another piece of unused memory, and then clear the first piece of memory. This algorithm for recycling objects is called the "mark-copy algorithm", which is based on the "mark-sweep algorithm" evolved.
    Advantages :
    ① Solve the problem of unstable execution efficiency of the "mark-sweep algorithm". The algorithm is mainly applied to the new generation. Most of the new generation objects are born and die. The new generation is divided into Eden, From Survivor, To Survivor , Eden and a Survivor are used for storage of new objects each time. During garbage collection, the surviving objects in Eden and From Survivor are copied to another Survivor, so that there will be no unstable execution efficiency, because most objects are born and die (this advantage is relative to the new generation Say). ②Solved the space fragmentation problem of "mark-sweep algorithm", because Eden From Survivor is cleared, there will be no more discontinuous space fragments.
    Disadvantages :
    ① "Mark-copy algorithm" because a part of the space must be free at all times, so there will be a certain amount of space waste. ② In extreme cases, the To Survivor area may not be able to store the objects that survive the new generation, so it needs to be allocated Guarantee strategy (the subject enters the old age).
    Application :
    The commonly used implementations of "mark-copy algorithm" are: Serial, ParNew, and Parallel Scavenge. The collection targets of these three garbage collectors are the new generation (the next article will introduce these three garbage collectors in detail).
    Insert picture description here

  3. Marking-sorting algorithm
    What is marking-sorting algorithm?
    The "mark-copy algorithm" has its own shortcomings. This scenario is very suitable for the young generation, but it is not applicable to the old age where most objects are survived. In the old age, there are too many objects to survive, and the memory overhead of the copy action It will be very large, so in view of the characteristics of the old age, the "marking-sorting algorithm" is proposed. The marking action is the same as the marking in the "marking-clearing algorithm" and "marking-copying algorithm". The accessibility analysis algorithm is used Mark the objects, and the finishing part is to move the surviving objects to one end of the memory space, and then directly clean up the memory area outside the boundary.
    Advantages :
    ①Solved the space fragmentation problem of "mark-clear algorithm". ② Solve the problem of "marking-copy algorithm" requiring distribution guarantees.
    Disadvantages :
    According to the strong generation hypothesis "the more garbage collected objects are, the more difficult it is to be recycled". Most of the objects in the old age are objects with the age of 16, which are difficult to be recycled, so it is adopted "Mark-Organize Algorithm" to move objects has a great impact on the throughput of the application, but the "Mark-Organize Algorithm" has to be used, because the "Mark-Sweep Algorithm" wastes a certain amount of space, and the "Mark-Copy Algorithm" There must be an allocation guarantee strategy, and space is wasted, and the "mark-copy algorithm" cannot meet the extreme situation that all objects in the old age survive.
    Application :
    The commonly used implementations of "marking-arrangement algorithm" are: Serial Old and Parallel Old. The collection targets of these two garbage collectors are the old generation (the next article will introduce these two garbage collectors in detail).
    Insert picture description here

  4. Comparing mark-sweep algorithm and mark-sweep algorithm
    Why compare these two algorithms separately, because in the old age there were both CMS garbage collectors implemented using "mark-sweep algorithm" and Serial implemented using "mark-sweep algorithm" Old, Parallel Old and other garbage collectors, and the "mark-copy algorithm" is typically only used in the new generation (G1 is an exception, which is a collector different from the typical garbage collector), so here is the comparison The pros and cons of the two algorithms. The fundamental difference between the "mark-sweep algorithm" and the "mark-sweep algorithm" is whether the object has moved when the object is recycled . In other words, the "mark-sweep algorithm" is a non- Mobile garbage collection algorithm, and "marking-sorting algorithm" is a mobile garbage collection algorithm. In the "mark-and-sweep algorithm", because the object does not move, but only the marked object is cleared, a lot of space fragmentation is generated. As a result, when a new object comes in, there may not be enough space to store the new object. GC is triggered frequently, and because the space is fragmented, the allocation of objects must rely on the "free list". Every time an object comes in, it must first use the free list to find out which areas in the heap are free and sufficient for the allocation of the object, which will increase invisibly The time cost of the program is reduced and the throughput of the application is reduced. Therefore, the garbage collector implemented by CMS as a "mark-sweep algorithm" pays more attention to the delay of the STW state rather than the throughput of the application. In summary, "mark "Clearing algorithm" is more complicated in the stage of object allocation . "Marking and sorting algorithm" is a mobile recycling algorithm. In the old age, most objects are alive. Therefore, when objects are recycled, a large number of objects will move. It will cause the object recycling phase to take a relatively large amount of time. Therefore, Parallel Old implemented by the "marking-sorting algorithm" is a garbage collector that pays more attention to throughput. In a word, the "marking-sorting algorithm" is recycling objects. The stage is more complicated .

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/114061740