Home Java JavaBase Detailed explanation of Java automatic garbage collection tutorial

Detailed explanation of Java automatic garbage collection tutorial

Jun 18, 2020 pm 01:21 PM
java Garbage collection

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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles