preface

In Java programs, the programmer does not need to display to free the memory of an object, but the virtual machine does it automatically. In the Jvm, a garbage collection thread, which is of low priority and does not normally execute, is triggered only when the virtual machine is idle or the current heap memory is low. It scans for unreferenced objects and adds them to the collection to be collected.

GC garbage collection is analyzed from three aspects

  1. Garbage collection judgment
  2. Garbage collection algorithm
  3. GC garbage collector

1. Garbage collection judgment

GC is done automatically by the JVM, and the timing of collection is uncertain depending on the JVM system environment. Of course, we can manually garbage collect the system.gc() method to tell the JVM to garbage collect, but the exact time of collection is uncertain.

Common scenarios for garbage collection:

(1) When the Eden region or S region is short of space (2) the old age space is insufficient (3) the method region is short of space (4) the system.gc() method

1.1 Object Reclamation

The heap contains almost all instances of an object, so how do you determine if an object can be reclaimed?

Generally, there are two ways to judge

    1. Reference counting: create a reference counter for each object. When there is an object reference, the counter is +1, when the reference is released, it is -1. When the counter is 0, it can be reclaimed. Disadvantages do not solve the problem of circular references.
    1. Reachability analysis algorithm: Start from THE GC ROOT ROOT and search down to search the path traveled. When the GC ROOTS are not connected to a chain of task references, it proves that the object can be reclaimed.

So when do you GC ROOTS?

  • Objects referenced in the VM stack
  • Object referenced in the local method stack JNI
  • Statically referenced objects in the method area
  • Object referenced by a constant in the method area

1.2. Class information Reclamation

The following three conditions must be met

  1. All instances of this class have been reclaimed
  2. The Class Loader that loaded the Class has been reclaimed
  3. The corresponding java.lang.class object for this class is not referenced anywhere

1.3. Constant recovery

Constant pool reclamation mainly reclaims discarded constants. So how do you know if a constant is deprecated? For example, if there is a String “ABC” in the constant pool, if there is no String object that references the String constant, then the constant “ABC” is discarded.

2. Garbage collection algorithm

2.1 Replication Algorithm

Divide the available capacity into two equal size pieces, and use only one piece at a time. When this piece of memory runs out, garbage collection is triggered to copy the remaining objects to the other piece, and then clean up the used memory space. Typical “space for time thinking”. For example, the partition of Eden, S1 and S2 in our young generation is the replication algorithm adopted.

2.2 Tag clearing algorithm

The algorithm is divided into two stages: “marking” and “clearing”. First, all the objects that need to be collected are marked out. After the marking is completed, the same marked objects are collected.

Disadvantages: Efficiency issues, marking and clearing efficiency are not high space issues, resulting in memory fragmentation

2.3 Tag sorting algorithm

Tag sorting, after which all objects are moved to one end and memory outside the boundary is cleaned up. Advantages: continuous memory Disadvantages: time consuming to move

2.4 Generational collection algorithm

Generational collection algorithm The core idea of the current approach adopted by most JVMS is to divide memory into different regions according to the life cycle of objects. Generational collection is a divide-and-conquer idea. The Java heap is typically divided into new generation and old generation. Select the appropriate garbage collection algorithm according to the characteristics of each generation.

3. Garbage collector

3.1 Serial garbage collector

Only one garbage collection thread executes, and the user thread suspends. It is suitable for embedded devices with small memory.

3.1.1 Serial(Young)(Tag Cleaning algorithm)

The new generation of single-threaded collectors, marking and cleaning are single-threaded, the advantage is simple and efficient

3.1.2 Serial Old(Tag Sorting Algorithm)

Old age single-threaded collector, old age version of Serial collector;

3.2 Parallel Garbage Collector

Multiple garbage collection threads work in parallel, but the user thread is still in the waiting state. This method applies to scenarios such as scientific computing and background processing.

3.2.1 ParNew (Young)

The multithreaded version of the Serial collector, the copy algorithm, needs to stop the Word

3.2.2 Paraller Scavenge (Young)

The new generation collector, the collector of copying algorithms, the collector of concurrent multithreading, aims to achieve a manageable throughput. If the virtual machine runs for a total of 100 minutes and garbage takes 1 minute, the throughput is 99%.

3.2.3 Paraller Old (Old)

Exploiture is an older version of the Parallel Scexploiture. It uses a multithreaded, mark-insane algorithm.

3.3 Concurrent Garbage collector

The user thread and the garbage collection thread execute simultaneously. The garbage collection thread executes without pausing the user thread

3.3.1 CMS (OLD)

3.3.2 G1 collector