This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

G1 garbage collection process

The garbage collection process of G1 mainly starts from the new generation collection, the new generation collection and concurrent marking, and then the mixed collection. Next, let’s talk about the first new generation collection.

G1 Yong Collection

At the beginning of our program, 5% of the space of the new generation will be allocated by default. Here, we assume that 8 regions are allocated to Eden and 1 Region to Survior (just for the convenience of drawing, Eden may actually have dozens or even hundreds of regions), then the corresponding initial memory allocation is as follows:

Then, when our Eden Region is full, GC of the new generation will still be triggered. GC of the new generation will still collect garbage through replication algorithm, and the system will enter the state of “Stop the World”. Then, the surviving objects in the corresponding Region of Eden Region will be copied to the corresponding Region of S1. Then the garbage object in Eden’s Region is reclaimed.

So when does the new generation of objects enter the old age? As before, there are still the following conditions: 1) The object has escaped multiple garbage collection in the new generation, and will enter the old age when it reaches a certain age. You can set the age by setting -xx: MaxTenuringThreshold

2) Dynamic age rule judgment: if after a Cenozoic GC is discovered, Survior of the same age exceeds 50%, such as 1-year-old, 2-year-old, 3-year-old, and 5-year-old, the sum of the 3-year-old objects has exceeded 50% of Survior. Then all the subjects aged 3 and above will enter the old age

So after a period of use and garbage collection by the new generation, some objects will always go into the old age, as shown below:

Now you might be wondering, right? Didn’t we say that we have large objects that will go straight into the old age according to the JVM’s space guarantee principle?

According to G1 allocation rules, G1 provides special regions for storing large objects, rather than adding large objects to old regions. In G1, large objects are determined based on the size of a Region. If the size of an object exceeds 50% of the Region size, It is then placed into a large object’s special Region, called humongous, as shown below:

When will the humongous object be recycled? It does not belong to the new generation and does not belong to the old age, when to trigger garbage collection for recycling?

In fact, it is very simple, in the new generation and old generation of the collection, along with the collection of large objects, so this is the G1 memory model of the allocation and collection of large objects strategy.

Note:

One of the things that G1 does with Cenozoic garbage collection is “initial tagging” : simply tagging objects that GC Roots can be directly associated with, in preparation for the next phase of concurrent tagging (similar to CMS garbage collection).

G1 Yong Collection + Concunrrent Mark

When G1 New generation garbage collection ends, the concurrent marking phase begins: the reachability analysis of objects in the heap is performed from GC Root, and the object graph in the heap is recursively scanned to find objects to be collected. This phase is time-consuming, but can be performed concurrently with the user program.

Furthermore, the JVM keeps track of changes made to the object during the concurrent marking phase, such as which object was created and which object lost its reference, as shown below:

G1 Mixed Collection

The G1 has a parameter: “- XX: InitiatingHeapOccupancyPercent”, the default value is 45%

In other words, when the size of the old generation occupies 45% of the Region of the heap, a mixed collection phase of the Cenozoic and the old generation will be triggered, and the full collection of E S 0 H will be carried out.

This phase, when triggered, causes the system to enter THE STW with the last flag:

  • Final marking phase: Based on the object modifications recorded in the concurrent marking phase, which objects are finally marked as alive and which are garbage

In this case, the old age is also recycled according to the mark-copy algorithm, and the object marked alive will be copied to the new Region as the old age Region:

Note that we mentioned one parameter above: -xx: MaxGCPauseMillis=time Specifies the pause time for the collection. The default is 200ms

Due to mixed recycling is a time-consuming operation, then according to the characteristics of the G1 can specify collection pause time, in order to keep the pause time this goal, the JVM will from the new generation, old age, as well as the large object H area is selected to copy the recycling part of the Region, if you don’t finish recycling, subsequent to recycle, recycle parts until completion of the recovery. However, the duration of a recycle pause is guaranteed to be another 200ms.

There is a parameter: “-xx: G1MixedGCCountTarget”, which can be set to perform a number of mixedgccounttarget in the last phase of a mixed collection. The default value is 8! The purpose of this setup is to remember the duration of each recycle pause so that the system can continue running at the same time.

There is also an argument: “-xx: G1HeapWastePercent”, which defaults to 5%, meaning that when a mixed collection is processed, it stops once the number of free regions reaches 5% of the heap memory.

Full GC

In the process of mixed reclamation, both the new generation and the old generation are implemented based on the replication algorithm. Therefore, the surviving objects in each Region need to be copied to other regions. If no free Region is found during the copying process, a failure is triggered. The system would immediately switch to our Seiral collector for single-threaded marking, cleaning, and compressing, and the process would be very slow!

Here we can summarize the FullGC for each collector:

  • SerialGC

    Garbage collection for out-of-memory generation occurrences – Minor GC

    Garbage collection occurring when memory is out of old age – full GC

  • ParallelGC

    Garbage collection for out-of-memory generation occurrences – Minor GC

    Garbage collection occurring when memory is out of old age – full GC

  • CMS

    Garbage collection for out-of-memory generation occurrences – Minor GC

    Full GC is triggered when a Concurrent Mode Failure occurs due to insufficient memory in the old age

  • G1

    Garbage collection for out-of-memory generation occurrences – Minor GC

    FullGC is triggered when the memory is insufficient and no extra Region is available for copying