Learn more about the JVM-G1 collector

preface

On a through case illustrates the common optimization and processing mode of old age, under this section to see the hottest G1 currently collector, G1 collector is JDK9 server, the default garbage collector while JDK9 now is not very popular, but learning the garbage collector is very important and very necessary.

These reviews

In the last section, we learned about the common optimization methods in the old era through an e-commerce simulation. At the same time, some common routines of old age optimization are summarized in the end, and the whole content is directly recalled with the summary in the following section:

- First of all, business objects are objects with a very short life cycle, and the pressure of the new generation is greater than that of the old age, so it is very cost-effective to appropriately reduce the space of the old age. - Predict the time when an object enters the old age in a high concurrency scenario. If the object often "spans", it means that some content space is wasted. That is, the Survior region - the approximate amount of memory an object needs in each partition, such as how much memory per thread - the age of an object to determine whether it needs to be changed, whether it is good or bad to age an object earlier - the impact of the collector on the garbage collection of an object, Also force the use of a garbage collector at startup, because the default garbage collector is different for different JDK versions.Copy the code

What are the pain points of the CMS+ParNew collector?

As we mentioned in the previous post, the traditional gold combo’s pain point is Stop world: In concurrent tags and concurrent finishing stage if allocate objects more than older generations to Full Gc, immediately stop all work on hand to garbage collection, and using the Serial processing, the old collector for users will find this card, obviously, will think system is stuck at the same time, the experience is very poor.

Introduction to the G1 collector

Why was the G1 born?

Interested can look at the white paper of G1 paper: citeseerx.ist.psu.edu/viewdoc/dow…

Why did G1 come into being? The biggest reason is that the JVM currently cannot allow the collector’s garbage collection to pause for a specific period of time, but it does not need to be as demanding as real-time. With this in mind, there were papers on G1 back in 2006, but it took years to implement! As you can imagine, this collector is bound to be very complex inside. Of course, we don’t need to study the bottom layer, just the basic principles.

History:

  1. Jdk7 update4 is commercially available
  2. Jdk8 update40 Supports concurrent uninstallation
  3. Jdk9 becomes the default garbage collector on the server
  4. Jdk10 rebuilds the “unified garbage collection interface” due to the coupling of the CMS garbage collector plug-in functions.

Features of the G1 collector

  1. Set an expected pause time for garbage. The most efficient collection is performed based on the Region size and collection value.
  2. The memory is not divided into new generation and old generation. Instead, the memory is divided into regions to realize dynamic generation based on system resources.
  3. The Region may belong to the New generation or the old age, and whether it is assigned to the new generation or the old age is controlled by G1 itself.
  4. Select the region with the minimum collection time and the most objects to collect for garbage collection.

Complements the features introduced in Understanding the JVM Virtual Machine:

  • No longer insist on a fixed size and a fixed number of partitions
  • Use region to divide regions of the same size and play different roles
  • Humongous regions: Designed to be used for large objects, the region with the highest value is reclaimed first according to the shortest pause time model. More than half of a region is a large object
  • The background uses a priority list to reclaim the areas with the largest revenue

The G1’s Region

G1 does away with the concept of fixed generations, and instead uses the concept of partitioning, which cuts memory into small chunks that are divided into new generation (Eden, Survior), old age, large object (Humongous), and so on.

G1 specifies that objects that are larger than half of the Region are large objects and do not participate in generation.

A Region is not always a new generation or an old generation. It is usually a free state. It is divided into specified generations and stores specific objects only when needed. You might wonder, does allocating regions not generate a lot of junk? The answer, of course, is no. Firstly, the new generation uses the replication algorithm to copy the surviving objects to a region for storage. At the same time, the surviving objects will not be copied if they are larger than a certain proportion of the region (as mentioned below), and the whole region will be cleaned up and recycled. In the same way, regions of garbage objects are directly cleaned up, which does not cause memory fragmentation. Finally, large objects are stored across multiple regions by special regions. Once the collection is completed, large objects are directly wiped out and corresponding regions are cleaned up.

How many regions are there and what is the size of each region?

Calculation method: heap size / 2048, the G1 collector can have a maximum of 2048 regions, and the size must be a multiple of 2. That is to say, 4 GB memory for each region is 2 MB size.

Here are some common G1 configuration parameters:

-XX:Heap Region Size: specifies the Region Size manually. Memory -xx :G1NewSizePercent: specifies the initial proportion of the new generation. The default value is 5%. -xx :G1MaxNewSizePercent: Specifies the maximum proportion of the new generation by defaultCopy the code

Common parameters:

  • -xx :G1MixedGCCountTarget: indicates how many times the last mixed collection is performed after one garbage collection. This parameter means that the last phase of garbage collection alternates between collection and system running, and the operation ends after eight collections by default.

  • -xx :G1HeapWastePercent: 5% by default. This parameter is also designed to minimize the pause time when garbage collection stops if the number of idle regions in a mixed collection exceeds 5%.

  • – XX: G1MixedGCLiveThreasholdPercent: the default value is 85%, says live objects is lower than 85% recycling operation. If the number of surviving objects in a region is greater than 85%, the cost of copying is very high, and this type of region is highly likely to enter the old age

  • -xx :G1MaxNewSizePercent: Indicates that the new generation occupies the maximum heap memory space. The default value is 60%. This parameter means that at the initial 5% of the new generation, the system operation can allocate up to 60% of the total system space to the new generation. If the value exceeds this value, the new generation reclamation will be triggered.

  • -xx :NewRatio=n: Sets the ratio of the new generation to the old generation. The default value is 2:1

How do I understand how G1 works?

To better understand the working mode pause time model of G1, here is a fairly vivid example to explain the working mode of G1:

We usually go to services more thoughtful restaurant, the waiter will often see our trash on the table of how much or how many leftover dishes, when garbage after more than a certain amount, the waiter will come and plates or trash, then convenient behind serving, at the same time also can let wash the dishes attendants busy busy and stopping work.

After that, you will consider whether you will be tired if you collect one more plate every time there is one, and whether you will wait until there is a certain amount of plate to collect, for example, the table can not put new dishes, or the plate piled too many.

At the end of the day, if the speed of the dishes can catch up with the speed of the food being served each time, then basic normal operation should be no big deal.

What systems does the G1 fit into?

G1 is suitable for systems including large memory systems, as if, in accordance with the traditional generation, such as new generation 8 g 16 gb of memory, old age 8 g this classification, although garbage pause long time, but once the new generation filled, recovery efficiency is very low, because the object and the GC ROOT very much, eventually led to the garbage collection card for a long time. Unlike G1, it only needs to start recycling when the new generation approaches the pause mode time according to the value of the pause model according to the algorithm, instead of recycling when the new generation is full.

G1 is also suitable for need low latency of the system, because of the low latency for the response of the system requirements is very high, more emphasis on response time and resource requirements of the system are higher at the same time, the generational in the large memory model and the theory of the machine will cause the garbage collection pause for a long period of time, for the service requirements of real-time response is very high.

What is the biggest improvement of G1 over Parnew+ CMS?

The biggest advance of G1 and its characteristic is the time pause model, which can control the time of Stop World. At the same time, the garbage collection time can be controlled within the expected setting.

Other advances:

  • Algorithm: G1 is based on the mark-collation algorithm and does not generate space debris. When allocating large objects, it does not trigger a FULL GC in advance because continuous space cannot be obtained.
  • Multithreading: Multithreading algorithms are better than CMS and can take advantage of multi-core performance

No faults with the G1?

The biggest problem of this collector is precisely the pause model. The details of the algorithm in it are very complex, so we can’t just calculate through the business, but according to the log and tool assistance to complete the tuning, G1 tuning is very troublesome.

The second is the design of Region. The design of Region itself consumes a lot of system resources for maintenance, which means that the G1 collector itself needs to occupy at least 10%-20% of the memory to maintain its normal work, such as computing the pause model, maintaining information such as cross-generation reference and GC ROOT. The details contained in this process are very complex. See the “Other Materials – Dry goods” section, so the G1 collector itself requires good machine performance before it is recommended.

Here’s a summary of G1’s shortcomings, according to the book “Understanding the JVM In Depth” :

  1. Each region requires a larger memory space to handle memory set consumption, and the memory consumed by TAMS is required to implement the snapshot function.
  2. CMS uses a post-write barrier, whereas G1 uses a pre-write barrier as well as a post-write barrier. Additional issues with object pointer changes using queues for asynchronous concurrent marking (time cost of final marking)
  3. Since THE CMS uses the cardset method for incremental updates, only the barrier card table reference needs to be written, but it causes the user thread to pause.

Garbage collection steps for G1

Initial mark:

Similar to the CMS garbage collector, stop the world operation is required in the initial case. Only the objects that can be referenced by GC ROOT are marked, and the whole process is very fast.

Here you can see the structure of space already and traditional fixed generational model has different, heap memory is divided into small pieces of region, the initial mark will according to the local variables in the stack references or transfer reference marks the initial GC ROOT object, such as the completion of this process can be faster, because just is a simple tag.

Concurrency markup:

This phase is also similar to the CMS, in that it can be run concurrently with the user thread, the system can normally allocate objects, and the garbage collection thread will reference the objects according to the GC ROOT, marking the living objects. At the same time, the changes to the object are marked and recorded. This phase consumes system resources, but concurrent with the system thread does not have much impact.

Final mark:

Final marking stage: This stage is similar to CMS, and it also needs to stop world. At this time, the system process needs to stop and stop the allocation of objects. Meanwhile, the garbage collector is responsible for the final marking and classification of objects, and decides which objects need to be garbage collected.

Filter recycling:

Filter recovery phase: a phase that requires critical memory. In this phase, the number of regions in the old period is calculated, the proportion of the surviving objects, and the efficiency of the collection is calculated.

This phase also requires stop World, which puts the garbage collection thread at full throttle to complete more garbage collection actions within the specified pause time range. At the same time, this phase is repeated several times and runs alternately with the system thread during the collection, which means that the collection needs to be paused.

In addition, the recovery stage not only recycles the old age, but also the new generation and large objects, which can be regarded as full GC in the true sense. For example, if there are 1000 regions in the old era and 800 regions need to be reclaimed, 800 regions will be reclaimed

Combining steps here explain part of the above parameters, from the picture above you can see, the thread and garbage collection system is running alternately, in the final step in the default system interact with the garbage collection threads run will eight times, assumes that the pause time is set to 200 ms, then every time within 200 ms recycle as much as possible, recycle immediately open system thread, Then it runs for a period of time and collects again, and so on. If more than 5% is collected four times in the garbage collection, it means that this phase is completed ahead of time, and it immediately enters the next garbage collection cycle.

Corresponding reclaim parameters:

  • -XX:G1MixedGCCountTarget: indicates how many times the last mixed collection is performed after one garbage collection. This parameter means that the last phase of garbage collection alternates between collection and system running, and the operation ends after eight collections.
  • -XX:G1HeapWastePercent: The default value is 5%. This parameter is also designed to minimize the pause time when garbage collection stops if the number of free regions in a mixed collection exceeds 5%.

The G1’s FAQ

When does G1 trigger Mixed GC

Parameters: – XX: InterfaceTestControllernitiatingHeapOccupancyPercent, the default value is 45%.

This means that if the old generation takes up more than 45%**, an operation called mixed recycle will be triggered. ** Mixed recycle means that the new generation and the old generation will recycle together, and the entire system thread will no doubt stop.

What should I do if the collection fails?

When the collection fails, the thread is stopped, and then the single-threaded serrial is used to mark up all of the region’s live objects, clean up, and then clean up the garbage objects. The process is very slow.

In fact, this process is similar to THE Corrurnet Mode Fail of CMS. However, because the memory model of G1 is completely different, it needs to recycle the new generation, old age and large objects. The details of the algorithm are more complex, and the sorting and recovery time is longer, which can be said to be Full GC in the true sense.

Does G1 still have Eden and Survior regions?

The answer is that although it is not necessary to specify the size of the Cenozoic and old ages controlled by G1, the region itself still divides the Cenozoic and old ages when running, but it is no longer fixed, so there are still Eden and Survior regions corresponding to the Cenozoic.

How is the new generation of G1 recycled?

Again using the replication algorithm, when the generation exceeds the default maximum of 60%, Minor GC is triggered, and then stop world is performed.

So here you might think isn’t it the same as before? As mentioned earlier, the feature of G1 is to specify the maximum pause time for garbage collection. G1 reclaims memory within the specified maximum reclamation time based on the Region size and reclamation prediction time. The reclaimed memory can be used by the new generation or the old generation.

How is the old G1 recycled?

It should be noted that G1 no longer has the concept of vintage collection. Instead, Mixed Gc is used, which is triggered when the number of old regions exceeds 45%.

How is G1’s collection different from previous generations of garbage collectors?

For example, if the new generation has 600 regions and 200 regions are recovered, these 200 regions are “free” and can be allocated to the new generation or to the old generation.

The pause time model of G1 will be reclaimed within 200MS according to user Settings, which can be passed: -xx :MaxGCPauseMills this parameter sets the maximum pause waiting time. G1 will track the region with the most value of collection according to this parameter for processing. However, this parameter is actually a soft target, and it does not mean that the collector is fully guaranteed to complete collection within this period. Instead, it means doing the most valuable collection in that time frame, just like the concept of the plate mentioned above, and it will do as much as possible to make the garbage collection speed as fast as possible to allocate.

When does an object enter the old age?

  1. An object escapes many garbage collections in the new generation and reaches a certain age. -xx :MaxTenuringThreashold allows you to set the age.
  2. Based on an overall Survior survival rate of more than 50%, the sequence is triggered when the size of an age object exceeds 50% of the Survior region.

When are large objects recycled?

Large objects do not belong to the new generation or the old generation. Therefore, the region memory of large objects will be reclaimed by the new generation or the old generation. This means that the collection of large objects relies on Mixed collection.

Other information:

Algorithm detail extract

Some notes from my own book, not suitable for text, so put them at the end (no one read them anyway, lol)

Region uses two AMS Pointers to allocate part of the Region space partition objects. All newly allocated objects exist in this interval. Similarly, if the reclamation fails to catch up with the memory allocation, user threads must be frozen and stop world is performed.

The predictable model depends on: -xx :MaxGcRauseMillion expected value, G1 will make the most efficient recovery within the expected value

Algorithm:

G1 uses the attenuated mean value algorithm to calculate the region collection time and dirty card data

  1. Reference value: mean value, standard deviation, confidence information
  2. The default pause time is 200 milliseconds, below which it is likely that garbage collection will not be fast enough to allocate objects

Dry goods article:

Zhihu: Understanding and analyzing the G1 collector principle

Consult the principle of the G1 algorithm

Meituan: Some key technologies of Java Hotspot G1 GC

Write at the end:

Finally, the JVM is really difficult, just read “Deep Understanding of the JVM” this book can only roughly understand the general principle of the current mainstream collector implementation, if you want to dig into the accumulation of a long time, of course, we do not need to learn so painful, this article talked about the content can cope with 80, 90% of the scenarios.

Next article a case according to tell me the roughly of the G1 optimization idea, pay attention to is only roughly, unlike previous generational collectors, G1 to resort to run tool, see the log, do good data analysis to tuning, because use the G1 collector system most not small, small system use generational and how concurrent traffic is also do not need tuning.