[JVM Advanced Road] Six: Garbage Collection Theory and Algorithms

In the front, we learned how the virtual machine judges that the object is recyclable, and then we understand some theories and algorithms of garbage collection in the Java virtual machine.

1. Generational collection theory

The generational collection theory is a set of rules of thumb based on the relationship between the number of surviving objects in the program and the age of the objects.

The relationship between the number of surviving subjects and age

It is based on two generational hypotheses:

  • Weak Generational Hypothesis (Weak Generational Hypothesis) : The vast majority of objects are dying .
  • Strong Generational Hypothesis (Strong Generational Hypothesis) : The more difficult it is for an object to survive the garbage collection process.

Summarize in popular words: Most stains are easy to wipe clean, and it is getting harder and harder to wipe clean without responsibility if you have not wiped it many times.

Based on this theory, the collector divides the Java heap into different areas, and then allocates the reclaimed objects to different areas for storage according to their age.

Specifically, the Java heap is divided into two areas: Young Generation and Old Generation. The young generation stores objects with a short survival time, and a small number of objects that survive each collection will be gradually promoted. Store in the old age.

Java heap memory division

For the new generation of objects, you can only focus on how to keep a small amount of alive instead of marking the large number of objects that will be recycled;

For the old age, you can reduce the frequency of garbage collection, and pay more attention to those objects that are going to die.

In order to reduce the cost of garbage collection, different garbage collection algorithms are used in the new generation and the old generation.

Based on generation, there are some types of garbage collection:

  • Partial collection (Partial GC): Refers to garbage collection where the goal is not to collect the entire Java heap, which is divided into:

    • Minor GC/Young GC: Refers to garbage collection where the target is only the young generation.
    • Old age collection (Major GC/Old GC): Refers to garbage collection that targets only the old age. Currently, only the CMS collector will collect the old generation separately.
    • Mixed collection (Mixed GC): Refers to the garbage collection that aims to collect the entire young generation and part of the old generation. Currently only the G1 collector will have this behavior.
  • Full GC: Collect garbage collection of the entire Java heap and method area.

2. Garbage collection algorithm

2.1, mark-clear algorithm

As the name knows, the 标记-清除(Mark-Sweep) algorithm is divided into two stages:

  • Mark : Mark out all objects that need to be recycled
  • Clear : Reclaim all marked objects

Mark-sweep algorithm

The mark-and-sweep algorithm is relatively basic, but there are two main disadvantages:

  • The execution efficiency is unstable. If the Java heap contains a large number of objects, and most of them need to be recycled, a large number of marking and clearing actions must be performed at this time. As a result, the execution efficiency of the two processes of marking and clearing increases with the number of objects. reduce.
  • Fragmentation of the memory space. After marking and clearing, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may cause insufficient contiguous memory to be found in advance when large objects need to be allocated in the process of program operation. Trigger another garbage collection action.

The mark-sweep algorithm is mainly used in the old generation, because there are fewer objects that can be reclaimed in the old generation.

2.2, mark-copy algorithm

The mark-copy algorithm solves the problem of low execution efficiency when the mark-sweep algorithm faces a large number of recyclable objects.

The process is also relatively simple: divide the available memory into two pieces of equal size according to the capacity, and only use one piece at a time. When this block of memory is used up, the surviving objects are copied to another block, and then the used memory space is cleaned up at once.

Mark-copy algorithm

This algorithm has an obvious shortcoming: part of the space is not used, and there is a waste of space.

The new generation garbage collection mainly uses this algorithm, because the new generation has fewer surviving objects, and only a small number of surviving objects are copied each time.

The specific implementation of a general virtual machine does not use a 1:1 ratio division. Taking HotSpot as an example, the HotSpot virtual machine divides the memory into a larger Eden space and two smaller Survivor spaces, and only Eden is used for each memory allocation. And a piece of Survivor. When garbage collection occurs, copy the surviving objects in Eden and Survivor to another Survivor space at one time, and then directly clean up Eden and the used Survivor space. The default size ratio of Eden and Survivor is 8:1.

Cenozoic memory partition

2.3. Marking-sorting algorithm

In order to reduce memory consumption, a targeted algorithm is introduced: 标记-整理(Mark-Compact) algorithm.

The marking process is still the same as the "mark-sweep" algorithm, but the subsequent steps are not to directly clean up reclaimable objects, but to move all surviving objects to one end of the memory space, and then directly clean up the memory outside the boundary.

Mark-up algorithm

The mark-organization algorithm is mainly used in the old age. In the old age, where a large number of objects live, moving objects is a great burden, and this type of object movement operation must be performed by suspending the user application (Stop The World).


reference:

[1]: "Deep Understanding of Java Virtual Machine: Advanced Features and Best Practices of JVM" by Zhou Zhipeng

[2]: Java Virtual Machine Series 2: Detailed explanation of garbage collection mechanism, animated pictures to help you understand

[3]: Geek Time "Deep Dismantling the Java Virtual Machine"

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/115274017