What is Minor GC/Major GC
Contents
[NOTE] Updated October 8, 2023. This article may have outdated content or subject matter.
What is Minor GC/Major GC
First, let’s popularize the classic heap layout of JVM:
For the classic JVM heap layout, there are two clear areas, the first is the Young area, which generally stores young objects or objects that have just been created. The second is the Old area, also known as the old generation, which generally stores longer-lived objects or objects promoted from the young area.
For the young area, we have three areas, one is the Eden area, and the other two are Survivor areas of equal size.
New objects are created in the Eden area.
Minor GC
At this time, if the new object cannot be created in the Eden area (the Eden area cannot accommodate it), a Young GC will be triggered. At this time, the objects in the S0 area and the Eden area will be analyzed for reachability together, find out the active objects, copy them to the S1 area, and clear the objects in the S0 area and the Eden area, so that those unreachable objects are cleared, and the S0 area and the S1 area are swapped.
But there is a problem here, Q: Why are there two Survivor areas?
A: Because imagine that if there is only one Survivor area, then it is impossible to implement garbage collection for the S0 area and the promotion of generational age.
Major GC
GC that occurs in the old generation, basically a Major GC will trigger a Minor GC. And the speed of Major GC is often 10 times slower than Minor GC.
When does Major GC occur
Since we already know that Minor GC is triggered when the Eden area is almost full
Q: What about Major GC?
A:
- For a large object, we will first try to create it in Eden. If it cannot be created, it will trigger Minor GC
- Then continue to try to store it in the Eden area, and find that it still cannot be put down
- Try to enter the old generation directly, and the old generation cannot put it down
- Trigger Major GC to clean up the space in the old generation
- If it fits, it succeeds
- If it doesn’t fit, OOM
See the image below:
Avoid frequent Full GC
- Avoid defining overly large objects (arrays)
- Avoid defining overly large objects as static variables
Author xiantang
LastMod 2023-10-08 (05461cf5)