Home >Java >javaTutorial >Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism

Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism

王林
王林forward
2019-08-24 14:21:572712browse

In the previous article, we said that automatic memory management is divided into two parts: allocating memory to objects and recycling the memory allocated to objects. In this article we talk about the latter, that is, recycling the memory allocated to the object. Recycling memory requires the use of a garbage collection mechanism, whose English name is GC (Garbage Collection).

In this part we have to solve the following questions:

1. Which memory needs to be recycled?

2. When will it be recycled?

3. How to recycle?

Which memory needs to be recycled?

The memory in the heap and method area needs to be recycled, and the rest does not need to be recycled.
Because only the heap and method area are shared by threads, the rest "live and die" with the thread. When the thread ends, the memory will naturally be recycled, so don't worry about them.

When will it be recycled?

(1) In the heap:

When the object "dies", its memory must be recycled. What does it mean that the object is dead? There's just no place to reference it, it's useless. So how do you tell if it's dead?

There are two methods:

Reference counting algorithm

Add a reference counter to the object. Whenever there is a reference to it, the counter's The value is 1. When the reference fails, the value of the counter is -1. When the value of the counter is 0, it means that the object is no longer referenced, that is, "it can die."

But there is a drawback, which is the problem of circular references. Just like the picture below, even if the two objects in the heap are useless, they cannot be recycled because they refer to each other and the counter value is at least 1.

Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism

reachability analysis

All generated objects are subtrees of a root called "GC Roots". Starting from GC Roots and searching downward, the path traveled by the search is called the reference chain. When an object has no reference chain to reach GC Roots, the object is said to be unreachable, that is, it can be recycled by GC. This is a commonly used method in Java.

Just like the unreferenced objects in the heap in the figure below, they can be recycled.

Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism

How to determine whether an object still has a reference? There are four types of references in Java:

Strong reference: Object o=new Object(). As long as the strong reference exists, the GC will never reclaim the referenced object.

Soft reference: describes some objects that are useful but not necessary. When the system is about to overflow memory, it will be recycled.

Weak reference: As long as GC is performed, it will be recycled.

Virtual reference: This is the weakest reference relationship. An object instance cannot be obtained through a virtual reference. Its function is to receive a system notification when this object is recycled by the collector.

(2) In the method area:


We know that what is stored in the method area has been loaded by the virtual machine Class information, constants, static variables, code compiled by the just-in-time compiler and other data. So we perform garbage collection in the method area, recycling some abandoned constants and useless classes.

How to determine whether a constant is obsolete?

Just look at the reference count. If no object refers to the constant, it means that the constant has been abandoned and can be recycled.

How to determine whether a class is useless?

There are three situations:

a. All instances of this class have been recycled.

b. The ClassLoader that loaded this class has been recycled.

c. The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere.

How to recycle?

There are 4 algorithms as theories:
• Mark-Sweep Algorithm
• Copy Algorithm
• Mark-Collation Algorithm
• Generational Collection Algorithm

There are 5 types of collectors as implementations:

Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism

Postscript

Memory overflow: The system cannot allocate any more Take out the space you need. For example, no more memory can be allocated to new objects in the heap, and if the stack is full, new stack frames cannot be pushed onto the stack.

Memory leak: If the memory is occupied by an object and is not returned, it is called a memory leak.

The above is a detailed explanation of the garbage collection mechanism in JVM. For more related questions, please visit the PHP Chinese website: JAVA Video Tutorial

The above is the detailed content of Detailed introduction to JAVA Virtual Machine (JVM) (3) - Garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

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