JVM garbage collection mechanism

JVM garbage collection mechanism

Three things to consider for garbage collection:

  • which memory needs to be reclaimed;
  • when to recycle;
  • how to recycle;

Why learn GC? When you need to troubleshoot various memory overflows. When the problem of memory leaks occurs, when garbage collection becomes the bottleneck for the system to achieve higher concurrency, we need to implement the necessary monitoring and adjustment of these "automated" technologies.

Algorithm for determining whether an object needs to be recycled

1. Reference counting

Add a reference counter to the object. Whenever there is a reference to it, the value of the counter is incremented by 1; when the reference is invalid, the value of the counter is decremented by 1; at any time, an object with a counter of 0 cannot be used.

The implementation of the reference counting method is simple and the judgment efficiency is high, but the reference counting method is not used to manage memory in the mainstream JVM. The main reason is that it is difficult to solve the problem of circular references between objects. for example:

MyObject1 obj1 = new MyObject1();
MyObject2 obj2 = new MyObject2();
obj1.innerField = obj2;
obj2.innerFiled = obj1;

obj1 = null;
obj2 = null;

At this time, the two objects will no longer be used, but because there are still references to each other inside the objects, the reference counter is not 0 and cannot be marked as recycled.

2. Accessibility Analysis

The basic idea is to use a series of objects called "GC Roots" as the starting point, and start searching down from these nodes, and the search path is called "reference chain". When an object does not have any GC Roots (that is, From GC Roots to this object is not reachable), it proves that this object is not available.

In the java language, objects that can be used as GC Roots include the following:

  • Objects referenced in the virtual machine stack (local variable table in the stack frame);
  • The object referenced by the static property of the class in the method area;
  • The object referenced by the constant in the method area;
  • Objects referenced by JNI in the native method stack;

Talking about the finalize() method

The JVM really needs at least two marking processes to determine whether an object needs to be reclaimed. If an object cannot reach the GC Roots, it will be first filtered and filtered once. The filtering condition is whether the object needs to execute finalize()methods. When the object does not override the finalize()method or finalize()the method has been called by the virtual machine, it is regarded as "not required to execute".

If it is necessary for the object to execute the finalize()method, then the object will be placed in a queue called F-Queue, and the virtual machine will execute the finalize()method later. The execution here will only trigger it, and does not promise to wait for it to finish running. This is done because if If an object finalize()executes slowly in a method, or an infinite loop occurs, it will likely cause other objects in the F-Queue to wait forever, or even cause the entire memory recovery system to crash. finalize()method is the object's last chance to escape death. The object in the F-Queue queue will be marked a second time by the GC later, and if the object successfully finalize()saves itself in the method, it will be removed from the "to be recycled" collection.

As can be seen from the above brush selection conditions, the finalize()method will only be automatically called once by the system. If the object faces the next recycling, its finalize()method will not be executed again.

It is not encouraged to use finalize()it, because its operation is expensive, uncertain, and the calling order of each object cannot be guaranteed. If you need to do work such as "closing external resources", it will be better to use try-catch or other methods. , more timely. This method is not a destructor in C/C++, but a compromise made when Java was first born to make it easier for C/C++ programmers to accept.

Garbage Collection Algorithms

1. Mark-Sweep Algorithm

The mark-sweep algorithm is divided into two stages: mark and clear: first mark all objects that need to be recycled, and then uniformly collect the marked objects after the mark is completed. It is the most basic collection algorithm, and the following collection algorithms are based on this idea and improve its shortcomings. There are two main shortcomings:

1) Efficiency problem, the efficiency of the two processes of marking and clearing is not high;

2) Space problem. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too many space fragments may cause that when a larger object needs to be allocated in the program running process, it cannot find enough continuous memory and has to trigger another time in advance. Garbage collection action.

2. Replication algorithm

The emergence of the replication algorithm is to solve the problem of efficiency. It divides the available memory into two equal-sized blocks according to the capacity, and only uses one of them at a time. When this piece of memory is used up, the surviving objects are copied to another piece, and then the used memory space is cleaned up at one time. In this way, the entire half-area is reclaimed every time, and there is no need to consider complex situations such as fragmentation during memory allocation. Just move the pointer at the top of the heap and allocate memory in sequence, which is simple to implement and efficient to run. It's just that the cost of this algorithm is that the memory is reduced by half. The price is a bit high.

Today's commercial virtual machines use this collection algorithm to recycle the "new generation", because most of the objects in the new generation are "dead", so there is no need to divide the memory space according to 1:1. This way you won't waste 50% of that much space.

3. Marking-Collating Algorithms

The replication algorithm will perform more replication operations when the number of surviving objects is high, and the efficiency will be lower. More critically, if you don't want to waste 50% of the space, you need additional space for allocation guarantees to deal with the extreme case that all objects in the used memory are 100% alive. Therefore, this algorithm cannot generally be used in the old age.

The mark-cleaning algorithm is the same as the mark-cleaning algorithm, but the subsequent steps are not to clean up the recyclable objects directly, but to move all surviving objects to one end, and then directly clean up the memory beyond the end boundary.

4. Generational collection algorithm

Currently, commercial virtual machines all use a generational collection algorithm, which divides the memory into several blocks according to the object life cycle. Generally, the Java heap is divided into the new generation and the old generation, so that the most appropriate collection algorithm can be adopted according to the characteristics of each generation. In the new generation, a large number of objects are found to die during each garbage collection, and only a few survive, so the replication algorithm is used , and the collection can be completed with only a small cost of copying the surviving objects. In the old age, because the object has a high survival rate and there is no additional space to allocate it, the mark-sweep algorithm or the mark-clean algorithm must be used for recycling.

garbage collector

If the garbage collection algorithm is the theory of memory recycling, then the garbage collector is the specific implementation of memory recycling. The HotSpot virtual machine provides the following garbage collectors. Each garbage collector has its own characteristics. Basic principles and usage scenarios, there is no best, only the most suitable, we need to apply the most suitable collector according to specific application scenarios.

Collectors suitable for the young generation:

Serial、ParNew、Parallel Scavenge、GI;

Collectors for the old age:

CMS、Serial Old(MSC)、Parallel Old、GI;

…loading and then sort out the features, principles and usage scenarios of each collector later

Record the notes from "In-depth Understanding of Java Virtual Machine" for your future review and reference

Guess you like

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