G1 GC pattern
The G1 comes in two modes
- The G1 offers two GC modes, The Young GC and The Mixed GC, both of which completely Stop The World
- Young GC: Selects regions in all Young generations. The time overhead of the Young GC is controlled by controlling the number of regions in the Young generation, i.e. the memory size of the Young generation.
- Mixed GC: Select all regions in the young generation, plus some regions in the old generation with high revenue according to global Concurrent marking statistics. Select the old Region with high income as far as possible within the cost target range specified by the user.
- Mixed GC is not Full GC, it can only reclaim part of old generation regions. If the Mixed GC cannot keep up with the program’s memory allocation speed, it will be unable to continue the Mixed GC when the old generation is filled up. The serial Old GC(Full GC) is used to collect the entire GC heap. So, in essence, G1 does not provide Full GC.
global concurre marking
- The implementation of Global Concurrent marking is similar to that of CMS, except that in the G1 GC, it mainly provides marking services for Mixed GC and is not a required part of a GC process.
- The implementation process of Global Concurrent marking is divided intoFour stages:
- Initial mark (STW) : This marks objects directly reachable from GC Root.
- Concurrent Marking: This phase marks objects in the heap starting with GC Root, executes concurrently with the application thread, and collects information on live objects for each Region.
- Remark (STw): Mark objects that change during the concurrent marking phase and will be recycled.
- Cleanup: Clears empty regions (those with no living objects) and adds them to the Free list.
- Phase one Initial Mark shares the pause of the Young GC because they can reuse root Scan operations, so global Concurrent marking can be said to have occurred with the Young GC.
- Phase 4: Cleanup just reclaims regions with no living objects, so it doesn’t need STW.
G1 main modes in operation
- YGC(different from CMS).
- Concurrent phase
- Mixed mode.
- The Full GC, which typically occurs when G1 encounters a problem, enables the standby collector, the Serial Old GC.
Mixed GC trigger timing
- Controlled by a number of parameters, it also controls which older regions are selected into the CSet.
- G1HeapWastePercent: After global Concurrent marking, we can know how much space in old Gen regions will be recycled, after each YGC and before Mixed GC occurs again, The Mixed GC will be checked to see if the garbage percentage reaches this parameter, and only if it does will the next GC occur.
Cset garbage collection.
- G1MixedGCLiveThresholdPercent: Indicates the proportion of live objects in the old Generation regionunder, will be selected into CSet.
- If I want the percentage of live objects to be 80%, and the result does not reach the set 80%, there are more garbage objects.
- G1MixedGCCountTarget: The maximum number of MixedGC executions after one GlobalConcurrent marking.
- G1OldCSetRegionThresholdPercent: most times Mixed GC can be elected to the CSet old generation number region.
Other parameters
G1 Overview of garbage collection
About the generational
- The G1 algorithm divides the heap into regions, which are still part of the generational collector. However, some of these areas contain the new generation, whose garbage collection still copies live objects to older generations or Survivor Spaces by suspending all application threads. The old era is also divided into regions.
- The G1 collector completes the cleanup by copying objects from one region to another. This means that, under normal processing, G1 compacts the heap (at least part of it) so that there is no CMS memory fragmentation problem.
Humongous area
Humongous, from a compound of huge and “bomb”.
- There is also a special region in G1 called the Humongous region. If an object occupies more than 50% or more of its partition capacity, the G1 collector considers it a giant object.
- These giant objects, by default, are directly assigned to the old age, but if it is a short-lived giant object, it will have a negative impact on the garbage collector. To solve this problem, G1 has a Humongous zone, which is dedicated to storing giant objects. If an H block does not fit a large object, G1 looks for contiguous H partitions to store it. Sometimes you have to start the Full GC in order to find consecutive H regions.
G1 Young GC
- The Young GC mainly GC the Eden region, which is triggered when the Eden space runs out. In this case, data in Eden space is moved to Survivor space. If Survivor space is insufficient, some data in Eden space will be directly promoted to the old space.
- Survivor zone data is moved to the new Survivor zone, and some data is promoted to the old chronosphere. Finally, the Eden space is empty, the GC completes its work, and the application thread continues.
- How do we find all root objects if we only GC new generation objects? Are all objects in the old world roots? That would take a lot of time to scan.So G1 introduced the concept of RSet. It stands for Remembered Set and tracks object references to a certain heap area.
- Except for the objects in the virtual machine stack and method area are GC Roots. In the generational garbage recovery model, when the young generation is recycled, the reference from the old age to the new generation can also be used as the root node of GC roots.
- In CMS, there is also the concept of RSet, an area in the old era that records references to the new generation. This is a point-out where, when scanning the root for a Young GC, you only need to scan this region, not the whole old age.
- Point-out refers to who I quote.
- In G1, however, point-out is not used because a partition is too small and there are too many partitions. If point-out is used, it will cause a lot of scanning waste, and some partition references that do not need GC are also scanned.
- The G1 uses point-in to solve this problem. Point-in means which partitions reference objects in the current partition. In this way, only scanning these objects as roots avoids invalid scans.Since there are multiple Cenozoic generations, do we need to record citations between Cenozoic generations? This is not necessary because every time GC is performed, all new generations are scanned, so only references between old and new generations need to be recorded.
- Point-in refers to who quotes me.
- It should be noted that, if there are many objects referenced, the assigner needs to process each reference, which will incur high assignment cost. In order to solve this problem, another concept, Card Table, is introduced in G1. A Card Table is a partitionLogically divided into contiguous areas of fixed sizeEach region is called a card. Cartoons are usually small, between 128 and 512 bytes. A Card Table is usually a byte array. The Card index (array subscript) identifies the spatial address of each partition.
- Read and write operations to the valuer – 51CTO.COM.
- By default, each card is not referenced. When an address space is referenced, the value of the array index corresponding to the address space is marked as ‘O’, which is marked as dirty and referenced, and the array index is also recorded by RSet. Generally, the RSet is a Hash Table. The Key is the start address of another Region, and the Value is a set. The element in the RSet is the Index of the Card Table
- G1-rset and Card Table – Jianshu.com
Young GC is divided into five stages
- Phase 1: Root scan
- Static and local objects are scanned
- Phase 2: Update the RSet
- Update the RSet of dirty Card queues
- Phase 3: Processing rSETS
- Detects objects from the younger generation to the older generation
- Phase 4: Object copy
- Copy surviving objects to survivor or old zones
- Phase 5: Processing the reference queue
- Soft reference, weak reference, virtual reference processing
Talk about Mixed GC
The global mark
- In G1 GC, global Concurrent marking, if provided for Mixed GCtagService is not a necessary part of a GC process. The implementation process of global current marking is divided into three partsFour steps
- Initial mark (STW) : This marks objects directly reachable from GCRoot.
- Concurrent Marking: This phase marks objects in the heap starting with GC Root, executes concurrently with the application thread, and collects information on live objects for each Region.
- Remark (STW) : Mark objects that change during the concurrent marking phase and will be recycled.
- Cleanup: Clears empty regions (those with no living objects) and adds them to the Free list.
When it comes to tags, the G1’s tricolor tagging algorithm has to be mentioned.
Three color marking algorithm
- We classify objects into three types:
- Black: The root object, or both the object and its children have been scanned (the object is marked and all of its fields are marked).
- Gray: The object itself is scanned, but children of the object have not been scanned (its field has not been marked or marked).
- White: unscanned objects. After scanning all objects, the final white objects are unreachable, that is, garbage objects (objects that are not marked).
G1 garbage collector details.
- All objects are iterated, and the ones that are white are garbage objects.
Missing mark problem of tricolor mark
- Because the tag thread and the user thread execute concurrently, an exception will be flagged.
- Looks normal marking.
- The user thread changed the reference to C immediately after the previous step was grayed.
- Since the reference was changed, the search for B was finished and turned from gray to black. Since A was already black, it means that the references pointed by A and A have been scanned and the search will not continue, resulting in the omission of C, as shown in the following figure. C is white, it will be recycled, it should not be recycled.
SATB was introduced to solve the loophole of trichromatic marking
It solves two problems with tricolor marking
- When a new object is created in concurrent markup, the creation of a new object will inevitably change the reference, which will affect the markup result.
- The user thread can change past references and also affect the tag result
Consider whether CMS is also due to concurrent marking, resulting in an impact on the accuracy of the results, so STW makes Remark.
SATB,
- In G1, SATB (snapshot-at-the-beginning) was used to record all objects as they were deleted.
- It has 3 steps:
- A snapshot graph is generated at the start of the tag, marking the live object.
- All changed objects are enqueued during concurrent marking (making all old references non-white in the write barrier). In the missing example above, C is an old reference to B, and becomes A non-white reference to A.
- There may be floating garbage that will be collected next time.
- SATB is a means of maintaining concurrent GC. The basis of G1 concurrency is SATB. SATB can be understood as taking a snapshot of the objects in heap memory before GC begins, at which point the live objects are considered alive, forming an object graph.
- At the time of GC collection, the new generation objects are also considered live objects, and other unreachable objects are considered garbage objects.
- How do I find objects allocated during GC (i.e., new objects generated during markup)? Each region records two top-at-Mark-start (TAMS: Pointers, prevTAMS and nextTAMS respectively. Objects above TAMS are newly assigned and are therefore considered implicitly marked. This way we find the newly allocated objects in the GC process and treat them as live objects.
- Now that objects are allocated during GC, what about references that change during GC? G1 offers a solution through a Write Barrier. A Write Barrier does extra processing for the assignment of a reference field. A Write Barrier lets you know which reference objects have changed and what.
- Mark is the process of iterating through the heap tag live object, using the three-color tag algorithm, The three colors are white(not yet accessed), gray(accessed but not yet fully scanned), and black(accessed and fully scanned). The whole three-color marking algorithm starts from GC roots and traverses the heap, marking white as gray for the reachable object. Then mark gray as black; After traversal all reachable objects are black and all whites are recyclable.
- SATB only performs “snapshot”(marked all reachable at markstart) at the beginning of marking. However, concurrent modification at a concurrent time may cause an object to miss marking.
G1 mixed recovery
Young generation and old generation GC
- G1 now knows which old partitions have the most garbage to recycle. At some point, the Mixed GC begins after the global concurrent markup is complete. These garbage collections are called “hybrid” because they not only do normal new generation garbage collections, but also collect some of the partitions marked by background scanning threads, which are Old regions with high returns.
- Hybrid GC is also a copy-clean strategy that refrees space when the GC is complete.
G1 generation algorithm
Unsuitable for the new generation
It’s just that G1’s generational algorithm isn’t suitable for the new generation
- The purpose of setting up partitions for the old era is that in the old era, some partitions have more garbage, and some partitions have less garbage, so that inWhen you recycle, you can focus on garbage-heavy partitions, hence the NAME G1.
- Partition means to divide roles into regions.
- However, this algorithm is not suitable for the new generation garbage collection, because the new generation garbage collection algorithm is a copy algorithm, but the new generation also uses the partitioning mechanism mainly for the convenience of generation size adjustment.
Pause (STW) prediction model
The G1 collector highlights a pause prediction model that selects the size of a CSet based on a user-configured pause time to achieve the desired application pause time. Set this parameter with the -xx :MaxGCPauseMillis parameter. This is somewhat similar to the ParallelScavenge. Pause times are not always as short as possible.
- A shorter set time means a smaller CSet per collection, leading to a gradual accumulation of garbage that eventually has to degenerate into Serial GC.
- If the pause time is set too long, it will cause a long pause every time, affecting the external response time of the program
Mixed GC degenerates into Full GC when it can’t keep up with the speed of object generation, and this is where tuning is important.
G1 Best Practices
- Is to use VM parameters to adjust G1 behavior.
Continuously tune pause time metrics
- -xx :MaxGCPauseMillis=x allows you to set the time for starting the application to pause, and G1 uses this parameter to select CSet when running to meet the response time Settings. In general, this value can be set to 100ms or 200ms (depending on the situation), but 50ms is not reasonable. If the pause time is set too short, G1 can’t keep up with garbage generation. Eventually degenerate to Full GC. So tuning this parameter is an ongoing process, gradually getting to the best state.
Do not set the size of new generation and old age
- The G1 collector adjusts the size of the new generation and the old generation as it runs. Change the generation size to adjust the rate at which objects are promoted and the age at which they are promoted to meet the pause time goal we set for the collector.
- Setting the Cenozoic size is like giving up the auto-tuning that G1 did for us. All we need to do is set the size of the entire heap and leave it to G1 to allocate the size of each generation.
Pay attention to Evacuation Failure
- Evacuation Failure is similar to a promotion Failure in a CMS where there is too much garbage in the heap space to complete copying between regions, and a Full GC has to be degenerated to do a global garbage collection.
Promotion is moving from the young to the old.
Demonstrate GC logging in code
Just look at it, it’s pretty much the same as the old GC log
There are just different names for the steps and stages of the collection
- The following are VM parameters
-XX:+PrintGCDetails // Prints GC logs
-XX:+PrintGCDateStamps // Prints the GC log timestamp
-XX:+UseG1GC // Use G1 collector
-XX:MaxGCPauseMillis=200m // Maximum pause time (STW) 200 ms
-Xmx20M // Maximum heap size
-Xms10M // Minimum heap size
Copy the code
G1 generally does not set the generation size
- code
public class G1Test {
public static void main(String[] args) {
int size = 1024*1024;
IntStream.range(0.11).forEach(i-> {
byte[] bytes = new byte[5* size]; }); }}Copy the code
- G1 from getting started to giving up reading GC Logs – Jianshu.com
Learn some of the mysteries of the GC process
What is the relationship between the collections
-
What are the connections between collectors in the figure below?
-
Seven classic garbage collector relationships.
What is Full GC, and what collector does it use?
- Full GC is defined as a global GC for the entire new generation, old generation, metaspace (java8 + replaced Perm Gen).
- Minor and Major GC are commonly known as Serial GC, Parallel GC, CMS GC, G1 GC and so on.
- The most important thing is to understand the above Hotspot JVM implementationWhat exactly do several combinations of GC algorithms contain.
- Serial GC algorithm: Serial Young GC + Serial Old GC (which is actually globally scoped Full GC);
- Parallel GC algorithm: Parallel Young GC + non-parallel PS Mark-sweep GC or Parallel Old GC (both of which are actually global Full GC), The PS MarkSweep GC or ParallelOldGC is controlled by UseParallelOldGC.
- CMS algorithms: ParNew (Young) GC + CMS (Old) GC (piggyback on ParNew) Never compaction (when a core CMS GC does not run when it dies) + Full GC for CMS
- G1 GC: Young GC + Mixed GC + Full GC(usually Serial Old) for G1 GC;
- Now that we know what these combinations are, let’s look at themTrigger conditions for various GC algorithms. In short, the trigger condition isThe region corresponding to a GC algorithm is full, or the prediction is almost full.For instance,
- Various Young GCS are triggered when Eden is full.
- The Serial Old MarkSweep GC/PS Parallel Old GC is triggered to predict that the total size of the objects promoted will exceed the residual size of the Old generation when the Young GC is to be performed.
- The trigger condition of CMS GC’s initial marking is that the usage rate of old generation exceeds a certain value.
- G1 GC initial marking is triggered when the Heap usage ratio exceeds a certain value, similar to CMS;
- The Full GC for CMS and Full GC for G1 GC are triggered by the obvious reason that their proud, unique algorithms are no longer available and only have to perform a global GC (trust me, it’s slow! This slow! It’s slow! ;
- PS MarkSweep GC(Serial Mark-sweep)/Parallel Old GC(Full GC) will run Parallel Young GC once;
- The reason is to lighten the load on Full GC.
Will only Full GC know STW?
- Serial GC: Full GC STW, Young GC STW
- Parallel GC: Full GC STW, Young GC STW
- CMS GC: Full GC STW, Young GC STW, Old GC only has two small stages STW.
- G1 GC: Full GC STW, Young GC STW, Mixed GC is composed of global concurrent markers and object replication. Global concurrent markers include two small phases STW and other concurrent markers.
So all garbage collection policies of all garbage collectors trigger STW, just for different lengths of time.