Wechat search “Java fish”, a knowledge point every day not bad
One knowledge point a day
What is the JVM garbage collection mechanism and when is the YoungGC or FullGC operation triggered
(a) about garbage recycling
JAVA garbage collection needs to accomplish three things:
1. Which memory needs to be reclaimed
2, when to recycle
3. How to recycle
The following are three questions to understand the Java garbage collection mechanism.
(2) what garbage needs to be recycled
Before garbage collection, the primary problem was to determine which garbage needs to be collected. Now Java uses GC Roots Tracing to determine whether an object is alive or not. This algorithm uses a series of objects named “GC Roots” as starting points and searches down from these nodes. When GC Roots cannot reach an object (or an object is not referenced by any other object), the object is deemed unusable, and the object is determined to be recycled.
As shown, ObjC is unreachable and this object is the object that needs to be reclaimed.
Objects that can be used as GC Roots in the Java language include the following:
1. Objects referenced in the virtual machine stack (local variable table in the stack frame)
2. The object referenced by the class static attribute in the method area
3. Objects referenced by constants in the method area
4. Objects referenced by Native method stack
(three) when to recycle
For how to recycle, I refer to Understanding the Java Virtual Machine. Unreachable objects in the root search algorithm are not recycled immediately, but are marked once:
If the object does not override Finalize (), or finalize() is already called, the virtual machine decides that this object is not necessary to finalize(), and the object will not be collected in this tag.
If this object is marked as necessary to execute finalize(), it will be placed ina Queue called f-queue and later garbage collected by the virtual machine.
However, the object has one last chance to escape. While in f-queue, the virtual machine makes a small mark on the objects in f-queue, and if an object is found reachable at this point, it will escape GC.
(four) how to recycle
The problem of how to recycle garbage boils down to how garbage collection algorithms recycle garbage. Three garbage collection algorithms are mainly introduced here:
4.1 Mark-Sweep Algorithm
This algorithm is divided into two stages, marking and clearing. Firstly, all the objects to be recycled are marked, and all the marked objects are recycled after marking.
The problem with this algorithm, as you can see from the picture, is that it creates a large amount of discontinuous space debris.
4.2 Copying Algorithms
The replication algorithm divides the memory into two parts. Each time one piece is used, the garbage is collected, and the remaining memory in the used memory is put into the other piece, and then the original memory block is emptied. The following figure shows the following memory block:Replication algorithm is widely used in garbage collection of the new generation. Since about 98 percent of objects of the new generation are to be recycled, the memory of the new generation will be divided into one Eden space and two Survivor Spaces, with a ratio of 8:1:1.
The first time YGC collects only Eden region, after which most objects (98% or so) are reclaimed, surviving objects enter Survivor0 through the replication algorithm (S0 and S1 are replaced later). After YGC again, the living objects in Eden +S0 enter S1. Again YGC, the living objects in Eden +S1 enter S0. In turn, cycle
4.3 Mark-Compact
The mark-tidy algorithm is divided into three steps: mark, tidy, and clean. The first step is also to mark recyclable objects, and then let the surviving objects move aside, and then directly clean up the garbage outside the boundary.
Tag collation algorithm is widely used in old garbage collection.
5. When to trigger YoungGC or FullGC
The YoungGC trigger will occur from time to time, when the New generation Eden field is full.
FullGC can be triggered in multiple cases:
1. Check before Young GC occurs. If “continuous memory space available in the old age” is < “Average size of sum of objects upgraded to the old age after Previous Young GC in the New generation”, it indicates that the size of objects upgraded to the old age after this Young GC may exceed the current available memory space of the old age. FullGC is triggered
A FullGC is triggered when there is not enough space for objects in the old generation
3. FullGC will also be triggered if the memory of the MetaspaceSize area reaches the set threshold -xx :MetaspaceSize=.