Simple understanding of VM garbage collection algorithm

1. Description

  • The three areas of the program counter, virtual machine stack, and local method stack are generated with the thread and destroyed with the thread. The memory allocation and recycling of these areas are deterministic
  • The memory allocation and collection of this part of the Java heap and method area are dynamic, and the garbage collector is concerned about this part of memory
New generation and old generation

The heap in Java is the largest memory space managed by the JVM, which is mainly used to store instance objects of various classes.
In Java, the heap is divided into two different areas: the young generation (Old) and the old generation (Old). The new generation (Young) is divided into three areas: Eden, From Survivor, To Survivor.
The memory model of the heap is roughly:


 
 
  • Heap size = new generation + old generation.

  • Garbage collection generally occurs in the java heap.

  • The method area is called the permanent generation.


Cenozoic:

Subject survival rate is low.

Old generation:

Subject survival rate is high.


2. Judge whether the objects in the heap are dead (objects that can no longer be used in any way)

2.1 Reference counting algorithm

meaning:

Add a reference counter to the object. Whenever there is a reference to it, the counter is incremented. When the reference is invalid, the counter is decremented. Any time the counter is 0, it is impossible to use the object.

Disadvantages:

It is difficult to solve the problem of circular references between objects.

Examples:

The following example: objA objects and objects have objB field instance, assigned to make objA.instance = objBand objB.instance = objA, in addition, the two objects without any reference to the fact that the two objects have been impossible to visit, but because they refer to each other with The other party, so that their reference counts are not 0, so the reference counting algorithm cannot recycle them through the GC collector.

public class ReferenceCountingGC {

    public Object instance = null; private static final int _1MB = 1024 * 1024; // 这个成员属性的唯一意义就是占点内存,以便能在GC 日志中看清楚是否被回收过 private byte[] bigSize = new byte[2 * _1MB]; public static void main(String[] args) { ReferenceCountingGC objA = new ReferenceCountingGC(); ReferenceCountingGC objB = new ReferenceCountingGC(); objA.instance = objB; objB.instance = objA; objA = null; objB = null // 假设在这行发生 GC,那么 objA 和 objB 是否能被回收 System.gc(); } } 

Note: VM options configuration parameters:-verbose:gc -XX:+PrintGCDetails

The partial print results are as follows:

[GC (System.gc()) [PSYoungGen: 6772K->680K(38400K)] 6772K->680K(125952K), 0.0098335 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [Full GC (System.gc()) [PSYoungGen: 680K->0K(38400K)] [ParOldGen: 0K->623K(87552K)] 680K->623K(125952K), [Metaspace: 3213K->3213K(1056768K)], 0.0145286 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

The result can be seen in the GC log 6772K->680K, which means that the virtual machine does not recycle the two objects because they refer to each other, indicating that the virtual machine does not use the reference counting algorithm to determine whether the object is alive.

2.2. Root search algorithm (reachability analysis algorithm)

meaning:

Through a series of objects named "GC Roots" as the starting point, search from these nodes down. The path that the search takes is called a reference chain. When an object is connected to GC Roots without any reference chain (using graph theory In other words, from GC Roots to this object is unreachable), it proves that this object is not available.

GC Rooot objects:
  • Referenced objects in the virtual machine stack (local variable table of the stack frame)
  • The object referenced by the class static property in the method area
  • Method to go to the object referenced by the constant
  • Objects referenced by JNI (that is, Native methods in general) in the local method stack

(In the method area and stack)

Examples:
 
 

Objects object5, object6, and object7 are related to each other, but they are unreachable to GC Roots, so they will be judged as recyclable objects.


3. Garbage collection algorithm

3.1 Mark-Clear Algorithm

meaning:

First mark all the objects that need to be recovered, and collect all the marked objects after the labeling is completed.

Disadvantages:
  1. low efficiency
  2. Too much space debris
Examples:
 
 

3.2 Copy algorithm (usually used in the new generation)

meaning:

Divide the available memory into two blocks of equal size according to capacity, and use only one of them at a time. When this block of memory is used up, copy the surviving objects to another block, and then clean up the used memory space once.

Disadvantages:

To reduce the memory to half of the original, the cost is too high.

Improve:

Divide the memory into a larger Eden space and two smaller Survivor spaces, each time using Eden and one of the Survivor. When recycling, copy the surviving objects in Eden and Survivor to another Survivor space at one time, and finally clear the space of Eden and Survivor just used.

Examples:
 
 

3.3 Marking-sorting algorithm (generally used in the old generation)

meaning:

Let all surviving objects move to one end, and then directly clean up the memory beyond the end boundary.

Examples:
 
 

3.4 Generational collection algorithm

meaning:

The memory is divided into several blocks according to the life cycle of the object. Generally, the Java heap is divided into the new generation and the old generation, and then the most appropriate collection algorithm is adopted according to the characteristics of each generation.

  • The new generation: replication algorithm
  • Old Generation: Mark-Organize / Mark-Organize

Now basically use this.


-----------------------------
Original link: https://www.jianshu.com/p/a0f4144356af
 

Guess you like

Origin www.cnblogs.com/suger43894/p/12673437.html