JVM learning garbage collection

What is "garbage"?

Something that has no use value for us is called "garbage". Objects that are useless in the JVM are called "garbage" because they take up our precious memory resources.

Where is the "rubbish"?

We've seen JVM memory earlier, so let's review it now. We know that the program counter, virtual machine stack, and local method stack are all private to the thread. These areas die with the death of the thread, which can achieve the effect of automatic cleaning. The Java heap and the method area are shared areas, and instances of various objects are stored in the Java heap. So there will be "garbage" in these two areas

Who is "garbage"?

How to determine who is garbage, here are two algorithms: reference counting algorithm, reachability analysis algorithm

reference counting algorithm

The idea of ​​reference counting algorithm is as follows:

Whenever a reference to it is made, the value of the counter is incremented by 1; when the reference is invalidated, the counter is decremented by 1. An object whose counter is 0 at any time cannot be used again.

This method is efficient, but there will be situations where it cannot be recycled. Please see the following code:

public class Main {
    public static void main(String[] args) {
        MyObject object1 = new MyObject();
        MyObject object2 = new MyObject();
 
        object1.object = object2;
        object2.object = object1;
 
        object1 = null;
        object2 = null;
    }
}
 
class MyObject{
    public Object object = null;
}

These two objects can no longer be accessed, but because they still refer to each other, the reference counts are not 0, and the garbage collector cannot collect them.

Reachability Analysis Algorithms

The basic idea of ​​​​this algorithm is: through a series of objects called "GC Roots" as the starting point, the search starts from these nodes, and the path traversed by the search is called the reference chain. When an object goes to the GC Roots without any When the reference chain is connected, it means that the object is not available.

Garbage Collection Algorithm

Mark-Sweep algorithm

This is the most basic garbage collection algorithm. The idea is simple. This algorithm is divided into two stages, one is "marking" and the other is "clearing".

The general idea of ​​the algorithm:

Mark all objects that need to be recycled, and recycle all marked objects uniformly after the marking is completed

Disadvantages: low efficiency, marking and clearing are not efficient; a large number of discontinuous memory fragments will be generated after marking and clearing.

Copying algorithm

This algorithm is simple, efficient, and not prone to fragmentation, but it reduces the available memory to half of the original memory. Let's see what this algorithm thinks:

Divide the available memory into two blocks of equal size according to the capacity, use one of them each time, when the memory is used up, copy the surviving objects to another block of memory, and then use the used memory once Sex clean up. Simple and crude.

Mark-Compact algorithm

The algorithm marking phase is the same as Mark-Sweep, but after completing the marking, it does not directly clean up the recyclable objects, but moves all surviving objects to one end, and then cleans up the memory beyond the end boundary

Generational Collection Algorithm

The generational collection algorithm is the algorithm used by most JVM garbage collectors at present. Divide the memory into several different regions according to the lifetime of the object. Under normal circumstances, the heap area is divided into the Tenured Generation and the Young Generation. The old generation is characterized by the fact that only a small number of objects need to be recycled during each garbage collection, while the new generation is characterized by the There are a large number of objects that need to be recycled, so the most suitable collection algorithm can be adopted according to the characteristics of different generations.

Memory allocation and reclamation strategy

We know that the allocation of objects is allocated on the Java heap. The objects are mainly allocated in the new generation Eden area, and may also be allocated in other areas (memory related parameters can be set).

Objects are allocated first in Eden

In most cases, objects are allocated in young Eden regions. Minor GC is triggered if there is not enough space allocated in the Eden area

Large objects go directly to the old age

What kind of objects are called large objects? The so-called large objects refer to Java objects that require a large amount of contiguous memory space. Typical large objects are long strings and arrays.

Long-lived objects will enter the old age

The virtual machine needs to solve the problem of which objects should be placed in the young generation and which objects should be placed in the old generation. To solve this problem, the virtual machine defines a counter for the object's age. If the object is still alive after Eden's birth and the first Minor GC, and can be accommodated by the Survivor, it will be moved to the Survivor space and the object's age will be set to 1. If this object can survive the Minor GC in the Survivor space, the age will be increased by 1 year. When the age increases to a certain level (the default is 15 years old), it will be promoted to the old generation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324421966&siteId=291194637