JVM memory management------Introduction to GC
Why do we need to understand GC strategies and principles?
The reason has actually been touched upon in the previous chapter. It is because in daily work and research, it is inevitable to encounter memory overflow and memory leak problems. If you encounter the problems mentioned above without understanding the GC strategy and principles, it will often make people feel at a loss.
After we understand the relevant knowledge, although sometimes we still cannot solve the problem quickly, what is certain is that at least we will not be at a loss.
What problems does the GC strategy solve?
Since automatic GC is to be carried out, there must be corresponding strategies. What problems do these strategies solve? Roughly speaking, the main points are as follows.
1. Which objects can be recycled.
2. When to recycle these objects.
3. What kind of recycling method is used.
[b]What algorithm is used in the GC strategy[/b]
Regarding the three questions mentioned above, in fact, the most important question is the first one, which is which Objects are recyclable.
There is a relatively simple and intuitive method, which is more efficient and is called the reference counting algorithm. However, this algorithm has a fatal flaw, that is, it cannot recycle objects with circular references. Imagine that if the JVM adopts this GC strategy, then when programmers write programs, code like the following should not be expected to appear again.
public class Object { Object field = null; public static void main(String[] args) { Thread thread = new Thread(new Runnable() { public void run() { Object objectA = new Object(); Object objectB = new Object();//1 objectA.field = objectB; objectB.field = objectA;// //to do something objectA = null; objectB = null;//3 } }); thread.start(); while (true); } }
This code seems a bit deliberate, but in fact, it often occurs in the actual programming process, such as two database objects in a one-to-one relationship, each maintaining a reference to the other. The last infinite loop is just to keep the JVM from exiting, and has no practical significance.
For the GC we are using now, when the thread thread ends, both objectA and objectB will be used as objects to be recycled. And if our GC adopts the reference counting algorithm mentioned above, these two objects will never be recycled, even if we explicitly classify the objects as null after use, it will have no effect.
Here is a rough explanation from LZ. In the code, LZ marked three numbers: 1, 2, and 3. After the statement in the first place is executed, the reference counts of the two objects are all 1. When the statement in the second place is executed, the reference counts of the two objects all become 2. After the statement in the third place is executed, that is, after both are classified as null values, the reference counts of the two are still 1. According to the recycling rules of the reference counting algorithm, the reference count will not be recycled until it reaches 0.
Root search algorithm
Due to the flaws of the reference counting algorithm, the JVM generally uses a new algorithm called the root search algorithm. Its processing method is to set up several root objects. When any root object is unreachable to a certain object, the object is considered to be recyclable.
Take the above picture as an example. ObjectD and ObjectE are related to each other. However, since GC roots are not reachable to these two objects, D and E will still be deleted in the end. As GC objects, if the reference counting method is used in the above figure, none of the five objects A-E will be recycled.
Speaking of GC roots (GC roots), in the JAVA language, the following objects can be used as GC roots:
1. Objects referenced in the virtual machine stack.
2. The object referenced by the class static property in the method area.
3. The object referenced by the constant in the method area.
4. The object referenced by JNI in the local method stack.
The first and fourth methods both refer to the local variable table of the method. The second expression has a clearer meaning. The third mainly refers to the constant value declared as final.
Garbage Collection Algorithm
The root search algorithm solves the basic problem of garbage collection, which is the first problem mentioned above and the most critical problem, which objects can be recycled .
However, garbage collection obviously still needs to solve the last two problems, when to recycle and how to recycle. Based on the root search algorithm, there are three main garbage collection algorithms in the implementation of modern virtual machines, namely the mark-clear algorithm, the copy algorithm, and the mark-sort algorithm. These three algorithms all extend the root search algorithm, but they are still very easy to understand.
Conclusion
The above is the content of JVM memory management------Introduction to GC. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive
