Home > Java > JavaBase > body text

Understand the concept of GC in JAVA in 3 minutes

angryTom
Release: 2019-11-30 16:36:32
forward
1651 people have browsed it

Understand the concept of GC in JAVA in 3 minutes

Preface

GC (Garbage Collection) is an inevitable hurdle in the process of learning JVM. Next, let's study GC systematically.

Understand the concept of GC in JAVA in 3 minutes

Before doing something, we must know why we are doing it. This does not only refer to GC, but also applies to our daily study and life. Know why. , only if you know why, can you fight without danger.

(Recommended video: java video tutorial)

Let’s first understand why there is a GC and what role the GC plays in the JVM. What effect did it have?

Why is there a GC

Students who have used C may know that the memory occupied by the object is occupied until the program ends and cannot be allocated until it is explicitly released. to other objects. If we do not manually clear these useless objects, the memory will soon be filled up. In the JVM, the GC plays the role of a scavenger. It can help us determine which objects are useless objects and how to proceed. Garbage collection, and the strategies that determine memory generation and memory allocation**.

Some students may ask, since our JVM will do GC work for us, why should we learn GC? Isn’t it good to leave everything to the JVM? Of course, in our daily life, we generally do not care about some details of GC, but when we encounter memory leaks, memory overflows, and high concurrency bottlenecks, we need to operate on GC and conduct more detailed analysis. Monitor and regulate.

Memory leak: refers to the dynamically allocated heap memory in the program that is not released or cannot be released for some reason, resulting in a waste of system memory, leading to serious consequences such as slowed down program running speed or even system crash. .

Memory overflow: There is unrecoverable memory in the application system or too much memory is used, which ultimately causes the memory used by the program to run greater than the maximum memory that can be provided.

Then the question now comes, we have to carry out garbage collection, first we need to know where the garbage is

Where the garbage is

In front We talked about the runtime memory area of ​​​​JVM, and we know that threads can be divided into thread exclusive areas and thread shared areas. The memory life cycle of the thread exclusive area (program counter, virtual machine stack, local method stack) is consistent with the thread, and The memory size allocated in these areas is related to the size of the class. In other words, when our class structure is fixed, this part of the memory will not change, and when the method or thread ends, the memory will naturally follow. The heap memory and method area in the thread shared area are different. The memory used in the heap memory and method area cannot be determined during compilation, because different implementations of an interface and a method The code executed by different control condition branches may be completely opposite. We only know which objects will be created at runtime. The allocation and recycling of this part of memory are dynamic, and our GC focuses on this part of the memory.

For example: If the JVM is a car, the thread exclusive area is like parts. The lifespan of these parts is basically known when they leave the factory, and the thread shared area is like gasoline. , the consumption of gasoline is related to the route we take, so the part we focus on is that this part will change dynamically, such as how to drive more fuel-efficient~

This article comes from php Chinese website,

java tutorial

column, welcome to learn!

The above is the detailed content of Understand the concept of GC in JAVA in 3 minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!