Home Java javaTutorial JVM memory management------Introduction to GC

JVM memory management------Introduction to GC

Dec 28, 2016 pm 03:31 PM
JVM内存管理

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);  
    }  
      
}
Copy after login

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.

JVM memory management------Introduction to GC

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)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles