Introduction to the
You’ll often see a variety of GC names, such as Minor, Young, Full, Old, Major, and Mixed GC.
After seeing so many GC terms at the beginning, I was really tired
Ah, no way, a voice after the quintessence of China can only slowly straighten them out.
The GC classification
Let’s categorize them one by one:
-
Partial GC: stands for partial garbage collection, which can be broken down as follows:
- Young GC (Minor GC) : Refers to garbage collection for the new generation region
- Old GC: refers to the collection of old ages, only CMS concurrent collection is in this mode
- Mixed GC: Collect the entire Cenozoic generation, and part of the old generation. Only G1 has this mode
-
Full GC: Collects the entire process, including the New generation, old generation, and permanent generation (before JDK8)
-
Major GC: can refer to old GC or Full GC. This is because the JVM specification does not have a specific definition for these terms, and over time they get confused. So if you need to specify whether you are talking about the old GC or the Full GC, for example, Zhou Zhiming’s “Deep Understanding of Virtual Machines” refers to the old GC as “Major GC/Full GC”. Old GC == Major GC
Object allocation
Let’s review the virtual heap memory map:
First of all, objects are generally allocated in the new generation, and in Eden area.
Large objects, such as arrays or long strings, tend to be allocated in the older generation. The VM provides the -xx :PretenureSize parameter, so that objects that are larger than this value are allocated in the older generation.
If no new objects can be created when the Eden pool reaches a certain proportion, the young GC is triggered to clean up useless objects and copy the remaining objects to a Survivor1(S1) with Eden pool emptied. When Eden is full again, the unemptable objects in Eden will be saved to another Survivor2(S2), and the unemptable objects in S1 will be copied to S2 to ensure Eden and S1 will be emptied. An object that is not cleared in Survivor is copied to the Old(Tenured) zone if it is repeated several times (default: 15). The age threshold for promotion can be set with -xx :MaxTenuringThreshold.
Trigger young GC
Since objects are normally allocated directly in the Eden region of the new generation, young GC (Minor GC) is triggered if there is not enough space in the Eden region. Because Java objects are mostly ephemeral, young (Minor) GC is frequent and fast.
Trigger a Full GC
Full GC takes at least 10 times longer to stop the world (STW) than Minor GC.
Calling the system.gc () method in your code will advise the JVM to perform Full GC, but note that this is only a recommendation. Whether the JVM executes or not is another matter, but in most cases it will increase the number of Full gc sessions and lead to System performance degradation. It is generally not recommended to do this manually. RMI can be disabled from calling System.gc by -xx :+ DisableExplicitGC.
YGC is preceded by a check to see if the maximum available contiguous space of the old age is greater than the total space of all objects of the new generation. If less, YGC is unsafe, and the HandlePromotionFailure parameter is checked to see if it is set to allow guaranteed failure, or Full GC is triggered if it is not. If allowed, the maximum available contiguous space of the old age is further checked to see if it is greater than the average size of objects promoted to the old age, and Full GC is also triggered if it is smaller
Metaspace (MetaspaceSize) is expanded when space is insufficient. FGC is also triggered when capacity is expanded to the value specified by the -xx :MetaspaceSize parameter
Major GC/Old GC
The Old GC is triggered in two ways: first, the allocation guarantee mechanism is enabled. If the memory size of objects entering the Old GC after the Minor GC is larger than the current memory size, the Old GC is triggered. After the Minor GC, there are too many objects left and the Old GC will be triggered.
Mixed GC
Mixed GC is a special concept in G1. When the memory of the old generation reaches 45%, the Mixed GC is triggered to collect both the new generation and the old generation.
Okay, that’s it for today, and I’ll talk about the G1 collector and CMS collector separately.
I’m a knife. I insist on sharing.