Garbage collection mechanism and algorithm explanation (1)

1. What is the principle of generation

Java heap is divided into generations (young generation and old generation), why should it be divided into generations? In fact, it is not difficult to understand that generational generation is to optimize performance. If generationalization is not performed, it will cause all objects to be rubbed together, so that the GC will perform a full scan of the heap area. Therefore, generational generation can greatly improve GC performance. Then, what is the principle of generational generation?

JVM adopts a generational collection strategy for heap garbage collection, so the principle of generation is to perform generations according to the life cycle of objects in the heap. In the young generation, a large number of objects die each time a garbage collection, and only a few survive. The survival rate of objects stored in the old age is high.

Must-know knowledge points

Young space: Young space (new generation), save objects with shorter life cycle
Tenured space: Old generation (old generation), save objects with longer life cycle
Minor GC/Young GC: Refers to garbage collection actions that occur in the new generation , Minor GC is very frequent, and the recovery speed is generally faster.
Major GC/Full GC: Generally, garbage in the old generation, young generation, and method area is collected. The speed of Major GC is generally more than 10 times slower than that of Minor GC.

Insert picture description here

1.1 Objects are allocated first in the Eden area

In most cases, objects are allocated in the Eden area in the new generation. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC.
Special case description : During the GC, the virtual machine found that the object could not be stored in the Survior space, so it had to transfer the objects of the young generation to the old generation in advance. The space in the old generation was enough to store the objects, so Full GC would not appear. After performing Minor GC, if the objects allocated later can exist in the eden area, memory will still be allocated in the eden area.

1.2 Big objects directly enter the old age

Large objects are objects that require a large amount of contiguous memory space (such as strings, arrays). JVM parameter-XX:PretenureSizeThreshold can set the size of a large object. If the object exceeds the set size, it will directly enter the old generation and will not enter the young generation. This parameter is only valid under the two collectors of Serial and ParNew. For example, set JVM parameters: -XX:PretenureSizeThreshold=1000000-XX:+UseSerialGC

1.3 Long-term surviving objects will enter the old age

Since the virtual machine adopts the idea of ​​generational collection to manage memory, it must be able to identify which objects should be placed in the young generation and which objects should be placed in the old generation during memory collection. In order to do this, the virtual machine gives each object an object age (Age) counter. If the object was born in Eden and survived the first Minor GC and can be accommodated by Survivor, it will be moved to the Survivor space, and the age of the object will be set to 1. Every time an object passes through MinorGC in Survivor, its age increases by 1 year. When its age increases to a certain level (the default is 15 years old), it will be promoted to the old age. The age threshold for the object to be promoted to the old age can be set by the parameter -XX:MaxTenuringThreshold.

1.4 Dynamic age judgment of subjects

In the Survivor area where the object is currently placed (one area, the s area where the object is placed), the total size of a batch of objects is greater than 50% of the memory size of the Survivor area, then at this time it is greater than or equal to the maximum age of the batch of objects Objects can enter the old age directly. For example, there are a group of objects in the Survivor area. The sum of multiple age objects of age 1 + age 2 + age n exceeds 50% of the Survivor area. The objects are placed in the old age. This rule actually hopes that those who may be long-term survivors enter the old age as soon as possible. The dynamic age determination mechanism of the object is generally triggered after the minor gc.

1.5 Survivor area of ​​objects surviving after Minor gc can't fit

In this case, some of the surviving objects will be moved to the old age, and some may still be placed in the Survivor area

1.6 Space allocation guarantee mechanism in the old age

Before the minor gc of the young generation, the JVM will calculate the remaining available space in the old generation. If the available space is less than the sum of the sizes of all objects in the young generation (including garbage objects), it will look at a "-XX:-HandlePromotionFailure" (jdk1 .8 is set by default) is the parameter set. If there is this parameter, it will look at the available memory size of the old generation and whether it is greater than the average size of the objects entering the old generation after each minor gc. If the result of the previous step is less than or the previously mentioned parameters are not set, then Full gc will be triggered, and the old generation and the young generation will be recycled together. If there is still not enough space to store new objects after recycling, "OOM" will occur. Of course, if the size of the remaining surviving objects that need to be moved to the old generation after minor gc is still greater than the available space in the old generation, then full gc will also be triggered. After full gc is finished, if there is still no space to put the surviving objects after minor gc, then "OOM" will happen

Insert picture description here

1.7 Eden and Survivor area default 8:1:1

A large number of objects are allocated in the eden area. When the eden area is full, the minor gc will be triggered. More than 99% of the objects may become garbage and be collected. The remaining survivor objects will be moved to the empty survivor area. Once the eden area is full, the minor gc will be triggered, the eden area and survivor will be recycled to the garbage object, and the remaining survivor objects will be moved to another empty survivor area at one time, because the objects of the new generation are all living and dying. , The survival time is very short, so the JVM default ratio of 8:1:1 is very suitable. Make the eden area as large as possible, and the survivor area can be used enough. The JVM defaults to this parameter -XX:+UseAdaptiveSizePolicy, which will cause this ratio Automatic change, if you don’t want this ratio to change, you can set the parameter -XX:-UseAdaptiveSizePolicy

2. How to judge the object can be recycled

There are almost all object instances in the heap. The first step before garbage collection on the heap is to determine which objects have died (that is, objects that can no longer be used in any way).

2.1 Reference counting method

Add a reference counter to the object. Whenever there is a reference to it, the counter is incremented by 1; when the reference is invalid, the counter is decremented by 1; any time the object with a counter of 0 is impossible to use. This method is simple to implement and efficient, but the current mainstream virtual machine does not choose this algorithm to manage memory. The main reason is that it is difficult to solve the problem of circular references between objects.

2.2 Reachability analysis algorithm

The basic idea of ​​this algorithm is to use a series of objects called "GC Roots" as the starting point. From these nodes, search downwards. The objects found are marked as non-garbage objects, and the remaining unmarked objects are garbage objects.
GC Roots Root node : local variables of the thread stack, static variables, variables of the local method stack, etc.
Insert picture description here

2.3 Common reference types

Java's reference types are generally divided into four types: strong reference, soft reference, weak reference, virtual reference

Strong references : ordinary variable references

public static User user = new User();

Soft reference : Wrap the object with SoftReference soft reference type object. Normally, it will not be recycled, but after the GC is done, it is found that no space can be released to store new objects, these soft-referenced objects will be recycled. Soft references can be used to implement memory-sensitive caches.

 public static SoftReference<User> user = new SoftReference<User>(new User());

Weak reference : wrap the object with the object of WeakReference soft reference type. Weak reference is similar to no reference. GC will directly recycle it, which is rarely used.

 public static WeakReference<User> user = new WeakReference<User>(new User());

Phantom reference : Phantom reference is also called ghost reference or phantom reference. It is the weakest kind of reference relationship and is almost unnecessary

2.4 finalize() method finally determines whether the object is alive

Even the objects that are not reachable in the reachability analysis algorithm are not "must die". At this time, they are temporarily in the "probation" stage. To truly declare an object dead, at least undergo a re-marking process. The premise of marking is that the object finds that there is no reference chain connected to GC Roots after reachability analysis.
1. Mark and filter for the first time.
The filtering condition is whether it is necessary for this object to execute the finalize() method. When the object does not cover the finalize method, the object will be recycled directly.
2. Mark for the second time.
If this object covers the finalize method, the finalize method is the last chance for the object to escape the fate of death. If the object is to successfully save itself in finalize(), just re-establish it with any object in the reference chain Just associate, for example, if you assign yourself to a certain class variable or member variable of an object, it will be removed from the collection that is "to be recycled" when it is marked the second time. If the subject has not escaped at this time, then basically it has really been recovered.

2.5 How to judge a class is useless

The method area mainly reclaims useless classes, so how to judge a class is useless? A class must meet the following three conditions at the same time to be considered a "useless class":

1. All instances of this class have been recycled, that is, there are no instances of this class in the Java heap.
2. The ClassLoader that loaded this class has been recycled.
3. 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.

Guess you like

Origin blog.csdn.net/mlplds/article/details/108517703