Home>Article>Java> What is the garbage collection mechanism of jvm?

What is the garbage collection mechanism of jvm?

青灯夜游
青灯夜游 Original
2023-02-01 14:02:26 11789browse

The garbage collection mechanism of jvm is GC (Garbage Collection), also called garbage collector. Basic principles of GC: Recycle objects that are no longer used in memory; the method used for recycling in GC is called the collector. Since GC needs to consume some resources and time, Java analyzes the life cycle characteristics of the object and follows the Objects are collected in the new generation and old generation to shorten the pause caused by GC to the application as much as possible.

What is the garbage collection mechanism of jvm?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

What is garbage collection

One of the advantages of Java compared to c and c language is that it comes with a garbage collector. Garbage collection refers to removing the heap memory from time to time. Clean up unreachable objects. Unreachable objects will not be directly recycled immediately. The execution of the garbage collector in a Java program is automatic and cannot be forced. The only thing the programmer can do is to suggest the execution of the garbage collector by calling the System.gc method. , but whether it can be executed and when it will be executed are unknown. This is also the main disadvantage of the garbage collector. Of course, this shortcoming is outweighed by the great convenience it brings to programmers.

Why garbage collection is needed

If garbage collection is not performed, the memory will be consumed sooner or later, because we are constantly allocating memory space without recycling. . Unless the memory is infinite, we can allocate it arbitrarily without recycling it, but this is not the case. Therefore, garbage collection is necessary.

jvm garbage collection principle

There is a heap area in the JVM runtime data area, and the heap is a huge object pool. A huge number of object instances are managed in this object pool, and some of the reference levels of objects in the pool are very deep. An interface that is frequently called generates objects at a very high rate per second. At the same time, the relationship between objects forms a huge network.

Java has been creating an atmosphere of infinite memory, but objects cannot only increase without decreasing, so garbage collection is required; then how does the JVM determine which objects should be recycled? Which ones should be kept? This requires the use of the JVM's garbage collection mechanism, which is what we often call GC (Garbage Collection), also called a garbage collector.

The basic principle of GC (Garbage Collection): recycle objects that are no longer used in memory. The method used for recycling in GC is called a collector. Since GC needs to consume some resources and Time, after analyzing the life cycle characteristics of the objects, Java collects the objects according to the new generation and old generation, so as to shorten the pause caused by GC to the application as much as possible

● To the new generation The collection of objects is called minor GC
● The collection of objects in the old generation is called Full GC
● The GC forced by actively calling System.gc() in the program is Full GC

Different Object reference types, GC will use different methods to recycle. JVM object references are divided into four types:
● Strong reference: By default, objects use strong references (instances of this object have no other object references) , will be recycled only when GC)
● Soft reference: Soft reference is an application provided in Java that is more suitable for caching scenarios (it will only be GCed when the memory is not enough)
● Weak Reference: It will definitely be recycled by GC during GC
● Virtual reference: Since virtual reference is only used to know whether the object has been GC

The object is marked as a garbage method

The memory structure of JVM includes five major areas:Program counter,Virtual machine stack,Local method stack,Heap area,method area. Among them, the program counter, virtual machine stack, and local method stack are three areas that are born and destroyed with the thread. Therefore, the memory allocation and recycling of these areas are deterministic, and there is no need to consider the recycling issue too much, because When the method ends or the thread ends, the memory will naturally be recycled. The Java heap area and method area are different. The allocation and recycling of this part of memory are dynamic, which is what the garbage collector needs to focus on.

1. Reference Counter

Reference counting is an early strategy in the garbage collector. In this approach, each object instance in the heap has a reference count. When an object is created, the object instance is assigned to a variable and the variable count is set to 1. When any other variable is assigned a reference to this object, the count is increased by 1 (a = b, then the counter of the object instance referenced by b is 1), but when a reference of an object instance exceeds the life cycle or is set to a At the new value, the reference counter of the object instance is decremented by 1. Any object instance with a reference counter of 0 can be garbage collected. When an object instance is garbage collected, the reference counter of any object instance it references is decremented by one.

Advantages:The reference counting collector can be executed quickly and is intertwined with the running of the program. It is more beneficial for real-time environments where the program needs not to be interrupted for a long time.
Disadvantages:Cannot detect circular references. If the parent object has a reference to the child object, the child object in turn refers to the parent object. This way, their reference count can never be 0.

What is the garbage collection mechanism of jvm?

Analyze the above code through reference counting:

What is the garbage collection mechanism of jvm?

What is the garbage collection mechanism of jvm?

3-What is the garbage collection mechanism of jvm?

##2. Reachability analysis

The reachability algorithm is an algorithm used by current mainstream virtual machines, and the program Treat all reference relationships as a graph. Starting from a node GC Roots, search for the corresponding reference node. After finding this node, continue to search for the reference node of this node. When all reference nodes are searched, the remaining nodes are They are considered to be nodes that are not referenced, that is, useless nodes. Useless nodes will be judged as recyclable objects.

In the Java language, the 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);
● In the method area Objects referenced by class static properties;
● Objects referenced by constants in the method area;
● Objects referenced by JNI (Native methods) in the local method stack.


What is the garbage collection mechanism of jvm?

It can be concluded that object instances 1, 2, 4, and 6 all have object reachability, that is, surviving objects and objects that cannot be recycled by GC. Although Caprice instances 3 and 5 are directly connected, no GC Roots are connected to them, that is, objects that are unreachable by GC Roots will be recycled by GC.

3. Garbage collection algorithm

1. Mark-clear algorithm

The basic idea of mark/clear algorithm is as follows It has the same name and is divided into two stages: "marking" and "clearing": first, mark all objects that need to be recycled, and after the marking is completed, all marked objects will be recycled uniformly.


Marking stage:The marking process is actually the process of the reachability analysis algorithm introduced earlier. It traverses all GC Roots objects and makes the objects reachable from GCRoots Each object is marked with an identifier, usually in the header of the object, and is recorded as a reachable object.

Clearing phase:The cleaning process is to traverse the heap memory. If it is found that an object is not marked as a reachable object (by reading the object header information ), it will be recycled.

What is the garbage collection mechanism of jvm?

The above figure is a schematic diagram of the mark/clear algorithm. In the mark phase, object B can be accessed from object GC Root 1, and object E can be accessed from object B. Therefore, GC Root 1 to B and E are all reachable. Similarly, objects F, G, J, and K are all reachable objects; in the cleanup phase, all unreachable objects will be recycled.

When the garbage collector performs GC, all Java execution threads must be stopped (also known as "Stop The World"). The reason is that when the reachability analysis is performed during the marking phase, the object reference relationship cannot remain during the analysis process. Changing conditions, otherwise the accuracy of reachability analysis results cannot be guaranteed. The application thread will not resume running until the mark is cleared.

Disadvantages of the mark/clear algorithm:
Efficiency issuesThe efficiency of the two stages of marking and clearing is not high , because both stages need to traverse the objects in the memory, and many times the number of object instances in the memory is very large, which is undoubtedly very time-consuming, and the application needs to be stopped during GC, which will lead to a very poor user experience.

Space problemAfter the mark is cleared, a large number of discontinuous memory fragments will be generated (as can be seen from the figure above). Too many memory space fragments may cause the need to When allocating larger objects, insufficient contiguous memory cannot be found and another garbage collection action has to be triggered early.

2. Copy algorithm

The copy algorithm divides the available memory into two equal-sized blocks according to capacity, and uses one block at a time. When this block of memory runs out, copy the surviving objects to another block of memory, and then clean up all the objects in this block of memory at once


What is the garbage collection mechanism of jvm?

The copy algorithm recycles the memory of the entire half area every time, which reduces the time of traversing the marked objects. When clearing the used area objects, there is no need to traverse, the entire area memory is directly cleared, and the surviving objects are copied. The reserved area is also stored in address order, which solves the problem of memory fragmentation. When allocating object memory, there is no need to consider complex issues such as memory fragmentation. You only need to allocate memory in order.

Disadvantages of the copy algorithm:
The copy algorithm is simple and efficient, optimizing the low efficiency of the mark and clear algorithm and the problems of memory fragmentation. There are disadvantages:
● Reduce the memory to half of the original size, which wastes half of the memory space. The cost is too high;
● If the survival rate of the object is very high, in an extreme case, assuming the object survival rate is 100%, then we need to The time cost of copying the object cannot be ignored.

3. Mark-Collate Algorithm

Mark-Collate Algorithm The algorithm is very similar to the Mark/Sweep algorithm. In fact, the marking process of the Mark/Collate algorithm is still the same as The mark/clear algorithm is the same, but the subsequent steps are not to directly recycle recyclable objects, but to move all surviving objects to one end, and then directly clean up the memory outside the end edge.
What is the garbage collection mechanism of jvm?

It can be seen that the recyclable objects are cleaned up after recycling, and the surviving objects are arranged in memory according to rules. In this way, when we allocate memory to a new object, the jvm only needs to hold the starting address of the memory. The mark/organize algorithm makes up for the memory fragmentation problem of the mark/clear algorithm and eliminates the high cost of halving the memory of the copy algorithm. It can be said to kill two birds with one stone.

Disadvantages of marking/organizing:
● Inefficiency: Not only must the surviving objects be marked, but also the reference addresses of all surviving objects must be organized. In terms of efficiency Rather than copying the algorithm.

4. Generational collection algorithm

The idea of the generational collection algorithm is to divide the memory into several blocks according to the different survival cycles of the objects. Generally, the Java heap is divided into The new generation and the old generation (there is also a permanent generation, which is a unique implementation of HotSpot. Other virtual machine implementations do not have this concept. The collection effect of the permanent generation is very poor, and the permanent generation is generally rarely garbage collected), so that you can The most appropriate collection algorithm is adopted according to the characteristics of each era.

Features:
The new generation: it is born and disappears, and its survival time is very short. Use replication algorithm to collect
Old generation: Survive after multiple Minor GCs and have a long survival period. Use the mark/clear algorithm or mark/organize algorithm to collect the old generation

Every time garbage collection is performed in the new generation, a large number of objects are found to have died, and only a few survive. Therefore, the copy algorithm is used to recycle the new generation, and only a small amount of effort is required. The collection can be completed at the cost of copying objects;
The survival rate of objects in the old generation is high and it is not suitable to use the copy algorithm. Moreover, if the old generation uses the copy algorithm, it will not have extra space for allocation guarantee, so marking must be used. /clean algorithm or mark/collate algorithm for recycling.

What is the garbage collection mechanism of jvm?

Objects in the new generation "live and die". Every time GC occurs, a large number of objects will die and a small number will survive. The replication algorithm is used. The new generation is divided into Eden area and Survivor area (Survivor from, Survivor to), and the size ratio defaults to 8:1:1.
For objects in the old generation, because the object survival rate is high and there is no extra space for allocation guarantee, the mark-clear or mark-complement algorithm is used.
Newly generated objects enter the Eden area first. When the Eden area is full, Survivor from is used. When Survivor from is also full, Minor GC (new generation GC) is performed to copy the surviving objects in Eden and Survivor from. Enter Survivor to, then clear Eden and Survivor from. At this time, the original Survivor from becomes the new Survivor to, and the original Survivor to becomes the new Survivor from. When copying, if Survivor to cannot accommodate all surviving objects, the objects will be copied into the old generation based on the allocation guarantee of the old generation (similar to a bank's loan guarantee). If the old generation cannot accommodate it, a Full GC (old generation) will be performed. GC).
Large objects directly enter the old generation: There is a parameter configuration in the JVM
-XX:PretenureSizeThreshold, so that objects larger than this setting value directly enter the old generation. The purpose is to avoid a large number of occurrences between the Eden and Survivor areas. Memory copy.
Long-term surviving objects enter the old generation: JVM defines an object age counter for each object. If the object still survives after Eden is born and passes the first Minor GC, and can be accommodated by the Survivor, it will be moved to the Survivor and age. Set to 1. If it has not survived a Minor GC, its age will be increased by 1. When its age reaches a certain level (the default is 15 years old, which can be set through XX:MaxTenuringThreshold), it will be moved to the old generation. However, the JVM does not always require that the age must reach the maximum age before being promoted to the old generation. If the sum of the sizes of all objects of the same age (for example, age is x) in the Survivor space is greater than half of the Survivor, all objects with an age greater than or equal to x will directly enter the old generation. , no need to wait until the maximum age requirement.

Generational recycling:

We use an object1 to illustrate its recycling trajectory in the generational garbage collection algorithm.

1. Object1 is newly created and was born in the Eden area of the new generation.
What is the garbage collection mechanism of jvm?
#2. Minor GC, object1 is still alive and moved to the From survivor space. It is still in the new generation at this time.

What is the garbage collection mechanism of jvm?

3. Minor GC, object1 is still alive. At this time, object1 will be moved to the ToSuv area through the copy algorithm. At this time, the age of object1 is 1.
What is the garbage collection mechanism of jvm?
4. Minor GC, object1 is still alive. At this time, the objects of the same age as object1 in the survivor have not reached half of the survivors, so at this time, through the copy algorithm, fromSuv and The Tosuv area is swapped, and the surviving objects are moved to Tosuv.
What is the garbage collection mechanism of jvm?
5. Minor GC, object1 is still alive. At this time, the objects of the same age as object1 in the survivor have reached more than half of the survivor (the toSuv area is full), and object1 is Moved to the old generation area.
What is the garbage collection mechanism of jvm?
#6. After object1 survives for a period of time, it is found that object1 cannot reach GcRoots at this time, and the old generation space ratio has exceeded the threshold, triggering majorGC (it can also be considered It is fullGC, but it needs to be contacted by the garbage collector). At this time, object1 is recycled. fullGC will trigger stop the world.
What is the garbage collection mechanism of jvm?
In the above new generation, we have mentioned the age of the object. The object survives in the survivor state and will not be immediately promoted to the old generation object to avoid causing trouble to the old generation. Large impact, they must meet the following conditions before they can be promoted:
1. After minor gc, the age of objects surviving in the survivor area will be 1. When it exceeds (default) 15, it will be transferred to the old generation.
2. Dynamic objects. If the combined size of all objects of the same age in the survivor space is greater than half of the survivor space, objects whose age is greater than or equal to that age can directly enter the old generation.

The above is the detailed content of What is the garbage collection mechanism of jvm?. For more information, please follow other related articles on the PHP Chinese website!

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