JVM combat: What are the common garbage collection algorithms?

insert image description here

JVM running data area

In the process of executing Java programs, Java will divide the managed memory into several different data areas, as shown in the following figure

Before JDK1.8, the method area was implemented by the permanent generation (the permanent generation was in the heap)
The JDK1.8 method area was implemented by the meta space (the meta space was in the local memory)

Before JDK1.8, the heap memory was divided into the new generation, the old generation, and the permanent generation
. After JDK1.8, the heap memory was divided into the new generation and the old generation. The default ratio was 1:2, and the new generation was divided into 1 Eden area + 2 Survivor areas (S0 area, S1 area), of which the default ratio of Eden area, S0 area, S1 area is 8:1:1
insert image description here
The heap and method area are data areas shared by all threads

Virtual machine stack, native method stack, program counter is a thread-isolated data area

Garbage Collection Algorithm

mark-sweep algorithm

insert image description here

First mark all objects that need to be recycled, and after the marking is completed, the marked objects are uniformly recycled. It can also be reversed, marking surviving objects and uniformly recycling unmarked objects

shortcoming

  1. Efficiency issues, both processes of marking and clearing are not efficient
  2. Space problem. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too many space fragments may cause that when a large object needs to be allocated during the program running process, it is unable to find enough continuous memory and has to trigger another garbage collection in advance. action.

mark-copy algorithm

insert image description here
Divide the available memory into two equal-sized chunks by capacity, and use only one of them at a time. When the memory of this block is used up, the surviving objects are copied to another block, and then the used memory space is cleaned up again.

advantage

In this way, the entire half-area is reclaimed every time, and there is no need to consider complex situations such as memory fragmentation during memory allocation. Just move the top pointer of the heap and allocate memory in sequence, which is simple to implement and efficient to run.

shortcoming

Reduce memory to half

improve

Divide the new generation into a larger Eden space and two smaller Survivor spaces (S0 and S1). Only Eden and one of the survivors are used each time memory is allocated. When garbage collection occurs, the objects that are still alive in Eden and Survivor are copied to another piece of Survivor at one time, and then the space of Eden and Survivor that has been used is cleaned up, and the cycle repeats. When the Survivor space is not enough to accommodate the objects surviving after a Minor GC, it needs to rely on other memory areas (mostly the old generation) for allocation guarantee

The iterative process is as follows

Eden+S0->S1 (move the surviving objects of Eden and S0 to S1)

Eden+S1->S0

Eden+S0->S1

mark-collate algorithm

insert image description here
In view of the survival characteristics of objects in the old age, some people have proposed a mark-sort algorithm. The marking process is still the same as the mark-clean algorithm, but the subsequent steps are not to directly clean up the recyclable objects, but to make all surviving objects go to one end of the memory space. move, and then directly clean up the memory outside the bounds.

Summarize

According to the characteristics of the new generation, the vast majority of objects are ephemeral, so the new generation generally uses the mark-copy algorithm

According to the characteristics of the old age, the object survival rate is relatively high, so the old age generally uses the mark-sweep and mark-sort algorithms. Mark-and-clean algorithms for throughput concerns and mark-to-clean algorithms for latency concerns

Reference blog

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/123419434