SoFunction
Updated on 2025-03-08

A brief analysis of the process of JVM garbage collection

There are many algorithms for JVM garbage collection, but no matter which algorithm it is, the general process when performing GC is similar, mainly with the following 3 processes:

1. Enumerate the root node

This process mainly involves finding all GC Roots objects, which generally occur in the JVM virtual machine stack frame, static objects in the constant pool, static class attribute references in the method area, and objects referenced in the local method stack. This process will occur STW, and all threads run to the Safe Region before they start executing.

There are usually two algorithms:

  • Reference counting method: add a reference counter to each object. Whenever there is a place to reference it, the counter value will be +1; when the reference fails, the counter value will be -1; any object with a counter of 0 at any time cannot be used.

The advantage is high efficiency, and the disadvantage is that the circular reference cannot be processed, resulting in memory overflow.

  • Accessibility analysis: Taking GC Roots as the root nodes, search downward from these root nodes. The path searched through is called a reference chain. When an object is not connected to any reference chain, it is proved that this object is unavailable.

Advantages can detect all objects, but disadvantages are inefficient.

GC Roots nodes are generally:

  • Objects referenced by stack frames in virtual machine stack
  • Object referenced by stack frame in local method stack JNI
  • Objects referenced in a constant pool
  • Objects to which static variables are applied in the class

2. Mark

The tagging process mainly marks which objects need to be recycled. Some GC algorithms are parallel, while others are executed with GC Roots tags. If it is parallel, STW will not occur.

If it is a GC algorithm for concurrent tagging, there will be a re-mark or final tag later. This is mainly to solve the problem that the user thread is still executing during the concurrent marking process, and there are changes in the object during this period.

There are two common marking algorithms:

  • Mark – Clear algorithm or Mark – Organization algorithm: Store a mark bit for each object, recording the state of the object (living or dead)
  • Copy algorithm: divide the memory into two parts evenly, and then use only one part of it at a time. When this part of the memory is full, copy all the surviving objects in the memory to another memory, and then clear the dead objects in the previous memory.

3. Clear or recycle

This stage will adopt different recycling strategies according to different GC algorithms.

  • When recycling, CMS algorithm will consider pause time to minimize the time taken by GC threads.
  • The G1 algorithm first sorts the recycling value and cost of each region, and formulates a recycling plan based on the expected GC pause time of the user.
  • Tag-clearing algorithm recycles objects in the second phase (clearing phase)
  • The copy algorithm is to clear objects that have not been copied in the current area by copying a surviving object to another memory area.

The above is a detailed analysis of the process of JVM garbage collection. For more information about JVM garbage collection, please pay attention to my other related articles!