Home  >  Article  >  Java  >  Detailed explanation of Java automatic garbage collection tutorial

Detailed explanation of Java automatic garbage collection tutorial

烟雨青岚
烟雨青岚forward
2020-06-18 13:21:203163browse

Detailed explanation of Java automatic garbage collection tutorial

Detailed Java Automatic Garbage Collection Tutorial

If used improperly in Java programming, no matter how large the memory is, it will be exhausted. This article will introduce to you one of them: how to save Java memory space and let the Java program automatically recycle garbage.

Point 1. Understand Java's automatic garbage collection

Garbage collection is a major feature of the Java language, which facilitates programming at the expense of performance. And garbage here is just useless objects. In C, programmers need to write their own destructor to release memory, which is troublesome, and may also be forgotten and cause memory leaks.

The memory allocation management of the Java language is determined by the internal mechanism of the JVM. Programmers can care less about its processing.

Point 2. The principle and significance of garbage collection

There is something called a garbage collector in the Java virtual machine. In fact, this thing may not really exist. , or it has been integrated into the JVM, but it doesn't matter, we can still call it a garbage collector.

The role of the garbage collector is to find and recycle (clean up) useless objects. In order to make the JVM use memory more efficiently.

The running time of the garbage collector is uncertain, determined by the JVM, and is executed intermittently during runtime. Although you can force garbage collection through System.gc(), there is no guarantee that the JVM will respond immediately after issuing this command. However, experience shows that after issuing the command, your request will be executed in a short period of time. The JVM usually performs garbage collection operations when it feels that it is short of memory.

Too frequent garbage collection will lead to performance degradation, and too sparse garbage collection will lead to memory shortages. This JVM will control it to the best, without programmers having to worry. But some programs will eat up a lot of memory in the short term, and these horrible objects will soon be used up. At this time, it may be necessary to force a garbage return command. This is necessary so that more physical memory is available.

As we learned from the above, useless objects are garbage. To be precise, an object is eligible for garbage collection when no threads access it.

For String, there is a string pool, which is beyond the scope of this article. The garbage collection and algorithm in the string pool are completely different from the garbage collection discussed here. But it has to be said that random splicing of strings often leads to a sharp decline in performance, especially in huge loop statements. Splicing strings is causing the program to commit suicide slowly. This is also a common mistake that many Java programmers make.

Since the string is a pool, it is for buffering and to have a higher hit rate, so the frequency of garbage collection may be much lower than that of the JVM object garbage collector.
What the garbage collector can only do is to ensure that the available memory is used as efficiently as possible so that the available memory can be managed efficiently. Programmers can influence the execution of garbage collection, but cannot control it.

Point 3: Influence garbage collection through programming

Although programmers cannot control the JVM's garbage collection mechanism. However, it can be affected through programming. The method of influence is to make the object eligible for garbage collection.

There are several types:

1. Assign the useless object to null.

2. Re-reference the variable Assignment. For example:

Person p = new Person("aaa");
  p = new Person("bbb");

In this way, the object new Person ("aaa") is garbage - it meets the conditions for garbage collection.

3. Let the interconnected objects be called "island" objects

Person p1 = new Person("aaa"); 
Person p2 = new Person("bbb"); 
Person p3 = new Person("ccc");  
p1=p2; 
p2=p3; 
p3=p1;  
p1=null; 
p2=null;
p3=null;

Before p1, p2, and p3 are set to null, the relationships between them It's a love triangle. If each is set to null, the love triangle relationship still exists, but the three variables no longer use them. Three Person objects formed an island, and finally died on the heap - being garbage collected.

4. Forced garbage collection System.gc()

In fact, the force here is the programmer’s wish and suggestion. When to execute it is the JVM’s garbage The recycler has the final say.

Calling garbage collection does not necessarily guarantee that unused objects will be deleted from memory.

The only thing guaranteed is that when you have very little memory, the garbage collector runs once before the program throws OutofMemaryException.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: http://community.itbbs.cn/thread/17817/

Recommended tutorial: "java video tutorial"

The above is the detailed content of Detailed explanation of Java automatic garbage collection tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:itbbs.cn. If there is any infringement, please contact admin@php.cn delete