Deep understanding of 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 age through the simulation of an e-commerce business. At the same time, in the end, some common routines of the old age optimization are summarized. The following section directly recalls the whole content:

- First of all, business objects are all 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 old age space - Forecast the time when the object enters the old age in a high-concurrency scene, if the object is often "cross-region", it means that part of the content space is wasted. It's called the Survior field -- how much memory is required for each partition of the object, for example, how much memory is required per thread -- the age of the object, whether it needs to be changed, whether it's good or bad to make the object old early -- the effect of the collector on object garbage collection, It is also mandatory to use a certain garbage collector on startup because the default garbage collector is different for different JDK versions.

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

In a previous post we mentioned the pain point of the traditional gold portfolio 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.

An introduction to the G1 collector

Why was G1 born?

Interested can look at the white paper of G1 paper: http://citeseerx.ist.psu.edu/… . (See comments that actually speak very abstract and general, I suggest in-depth study of the G1 to have a general look)

Why was G1 born? The biggest reason is that the JVM currently cannot make the collector’s garbage collection pause for a specific period of time, but it does not need to be as strict as real-time. With this in mind, the G1 paper was written as early as 2006, but it took years to implement it! As you can imagine, the inside of this collector is bound to be very complex, of course we don’t need to delve into the underlying, just its basic principles.

History:

  1. JDK7 Update4 is commercially available
  2. JDK8 Update40 Concurrent Type Uninstall support
  3. JDK9 becomes the default garbage collector on the server side
  4. JDK10 refactored the “Unified Garbage Collector Interface” due to the coupling of CMS Garbage Collector Plug-in functions.

Features of the G1 collector

  1. Sets an expected pause time for garbage. The most efficient collection is based on the Region size and recovery value.
  2. Memory is no longer fixed to divide the new generation and the old age, but is partitioned by Region for memory to achieve dynamic generation according to system resources.
  3. Region may belong to Cenozoic or Cenozoic, and the allocation to Cenozoic or Cenozoic is controlled by G1 itself.
  4. Select the region with the minimum collection time and the most collected objects to conduct garbage collection operation.

Additional features introduced in “Deep Understanding the JVM Virtual Machine” :

  • No longer insist on a fixed size and a fixed number of partitions
  • Region is used to divide regions of equal size, and different regions play different roles
  • Humongous region: Designed to be used for large objects, the region with the highest value is first retrieved according to the shortest pause time model. More than half the size of a region is large
  • The backend uses a priority list to prioritize the areas that generate the most revenue

The G1’s Region

G1 abandons the notion of fixed generations in favor of partitioning, dividing memory into smaller chunks, which can be divided into the new generation (Eden, Survior region), the old age, the large object (Humongous region), and so on.

G1 specifies that objects greater than half of Region become large objects and do not participate in generation.

Region is not fixed to new or old ages, but is generally free and is only divided into designated generations and holds specific objects when needed. There may be a question here, does this allocation of Region not produce a lot of garbage fragmentation? The answer, of course, is no. First of all, the new generation uses the replication algorithm, which copies the living objects to a region for storage. At the same time, the living objects greater than a certain proportion of the region will not be copied (mentioned below), and the whole region is cleaned up and recycled, while the old generation uses the marker-collation algorithm. In the same way, garbage object’s region will be cleaned up directly, and memory fragmentation will not be generated. Finally, large objects are stored across multiple regions and by special regions. Once recovered, large objects will be cleaned up directly to the corresponding region.

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

The G1 collector can have a maximum of 2048 regions and must be a multiple of 2 in size. That is to say, the size of each region given by 4G memory is 2M.

Here are some common configuration parameters for G1:

-XX:Heap Region Size: Manually set Region Size. -XX:G1NewSizePercent: Manual specifies the initial portion of the new generation, default is 5% -XX:G1MaxNewSizePercent: default is 60% of the new generation

Common parameters:

  • -XX:G1MixedGCCountTarget: Indicates how many times the last mixed collection is executed after a garbage collection. This parameter means collection and system running in the last phase of garbage collectionAlternate running, and end the operation after 8 reclaims by default.
  • -XX:G1HeapWastePercent: Default is 5%. When you mix the collection, if you mix the collectionIdle region exceeds 5%Stop garbage collection immediately, this parameter is also designed to minimize the pause time.
  • -XX:G1MixedGCLiveThreasholdPercent: The default value is 85%, indicating that the alive object must be less than 85% for the collection operation. Because if a region has more than 85% alive objects, the cost of copying is very high, and there is a high probability that such a region will be old
  • -XX:G1MaxNewSizePercent: The new generation takes up the maximum heap memory space, the default value is 60%. This parameter means that at most 60% of the system’s total space can be allocated from the system to the new generation for the initial 5% of the new generation, above which the new generation collection is guaranteed to be triggered.
  • -XX:NewRatio=n: Configure the ratio of old to new. Default is 2:1

How do you understand how the G1 works?

To better understand the pause time model of the G1, here is a vivid example of how the G1 works:

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 think about whether you will be tired if you pick up every extra plate, and whether you will wait until there is a certain amount of plates. For example, there is no room for new dishes on the table, or there are too many plates stacked.

Finally, if the speed of each dish can catch up with the speed of the dish, then the basic normal operation is not a big problem.

What kind of system does the G1 fit?

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. But G1 is different, it just needs to judge according to the algorithm and start recycling immediately when the new generation approaches the pause mode time according to the value of the pause model, instead of waiting for the new generation to be 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’s the biggest improvement in G1 over Parnew+ CMS?

One of the biggest improvements in G1 is the time pause model, which allows you to control the time of the Stop World. At the same time, you can keep the garbage collection time within the expected set range.

Other improvements:

  • Algorithm: G1 is based on mark-collate algorithm, which does not generate space fragmentation and does not trigger a Full GC prematurely when allocating large objects without obtaining contiguous space.
  • Multithreading: Multithreading algorithms are better than CMS and can take advantage of multi-core performance

The G1 has no drawbacks?

The biggest problem of this collector is precisely the pause model, the algorithm details are very complex, so we can not only debug through business calculation, but according to the log and tool assistance to complete the tuning, the tuning of G1 is a very troublesome thing.

The second is the design of Region, Region design itself to consume 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 their normal work, such as computing pause model, to maintain cross-generation reference and GC ROOT information, which contains very complex details. The G1 collector itself requires good machine performance to be recommended for use, as you can see in the “Dry Goods” section.

Here’s a summary of G1’s shortcomings, according to the book “Inside the JVM Virtual Machine” :

  1. Each region requires a larger memory space to deal with the consumption of memory sets, and part of the memory consumed by TAMS is also needed to implement the function of snapshot.
  2. Whereas CMS uses post-write barriers, G1 uses not only post-write barriers but also pre-write barriers. Additional object pointer changes for asynchronous concurrent markup using queues (time overhead for final markup)
  3. Since the CMS uses the card-set approach for incremental updates, only the barrier card table references need to be written, but this causes the user thread to pause.

G1 garbage collection steps

Initial mark:

Similar to the CMS garbage collector, the Stop the World operation is required in the initial case. Marking only the objects that GC Root can reference is a very quick process.

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.

Concurrent flags:

This phase is similar to CMS in that it can run concurrently with the user threads, the system can allocate objects normally, and the garbage collection thread can reference objects according to the GC ROOT, marking the surviving objects. At the same time, the change of the object is marked and recorded. This stage will cost the system resources, but the impact of concurrency with the system thread is not great.

Final mark:

Final marking phase: Similar to CMS, this phase also needs to stop the world. At this stage, the system process needs to pause to stop the allocation of objects, while the garbage collector is responsible for the final marking and sorting of objects to determine which objects need to be garbage collected.

Screening and recycling:

Filter recovery stage: a stage that requires intensive memory. This stage calculates how many regions are alive in the old age, the proportion of living objects, and the efficiency calculation of collection.

This phase also requires Stop World, which causes the garbage collector thread to run at full power to complete more garbage collection actions within the specified pause time. This phase is repeated several times and runs alternately with the system thread during the collection, which means that the collection is paused.

In addition, the collection stage not only recycles the old age, but also recycles the new generation and large objects, which is a full GC in the true sense. For example, in the old era, there were 1000 regions, but 800 regions need to be recovered by calculation, then 800 regions will be collected

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, It then runs for a period of time and then collects again, and so on. If more than 5% is collected in 4 cycles, it means that this phase has been completed prematurely and the next garbage collection cycle is immediately carried out.

Corresponding recovery parameters:

  • -XX:G1MixedGCCountTarget: Indicates how many times the last mixed collection is executed after a garbage collection. This parameter means that the operation ends after the last phase of the garbage collection, which alternates between the collection and the system run, and after the collection has been completed eight times.
  • -XX:G1HeapWastePercent: Default is 5%. Garbage collection operations are stopped immediately if the free region of a mixed collection exceeds 5% during a mixed collection. This parameter is also designed to minimize pause time.

The G1’s FAQ

When does G1 trigger Mixed GC

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

This means that if the old age takes up more than 45%, an operation called mixed collection is triggered. Mixed collection means that the new and old age are collected together, and the entire system thread stops without a doubt.

What if the collection fails?

If the collection fails, the thread will stop and then mark, collate, and clean up the garbage objects through a single-threaded Serrial manner for all Region alive objects, the whole process is very slow.

In fact, this process is similar to the Corrurnet Mode Fail of CMS. However, since the memory model of G1 is completely different, it needs to recover the new generation, old age and large objects, and the details of the algorithm are more complex, and the sorting and recovery time is longer. In other words, G1’s collection is a true Full GC.

Does G1 have Eden and Survior regions?

The answer is that although it is not necessary to specify that the size of the Cenozoic and the Survior is controlled by G1, the region itself will still divide the Cenozoic and the old ages during operation, but it is no longer fixed, so the Cenozoic still has corresponding Eden and Survior regions.

How is the new generation of G1 recycled?

Again, using the replication algorithm, when the new generation exceeds the default 60% maximum percentage limit, a Minor collection is triggered and a Stop World operation is performed.

And you might think, well, isn’t this the same thing as before? As mentioned earlier, the feature of G1 is to specify a maximum pause time for garbage collection. G1 will recover memory as much as possible within the maximum collection time specified by us according to the size of Region and the collection prediction time. The recovered memory can be used by the new generation or the old generation.

How is the old G1 recycled?

It is important to note that G1 no longer has the concept of old age collection. Instead, there is a Mixed GC, which is triggered when old region accounts for more than 45%.

What is the difference between G1 collection and previous generations of garbage collectors?

First of all, we need to make clear a concept, that is, region is generational storage, but it does not mean that it is always generational region. For example, if the new generation has 600 regions, 200 regions are recovered, and these 200 regions are equivalent to “free”, which can be distributed to the new generation or to the old generation.

The pause time model of G1 will recycle according to the user’s Settings, such as 200MS, which can be used: -XX: The parameter MAXGCPAUSEMILLS sets the maximum pause waiting time. G1 will track the region with the most recoverable value for processing according to this parameter. However, this parameter is actually a soft target, which does not mean that the collector completely guarantees the completion of collection within this time period. It means to do the most valuable recycling in this period of time. Just like the concept of the plate mentioned above, he will try his best to make the garbage collection speed up to the distribution speed.

When does the object age?

  1. The -XX: maxTenuringTHreashold parameter can set the age of the object after it has reached a certain age after avoiding many garbage collections in the new generation.
  2. According to the overall survival rate more than 50% of judging Survior, when sorting found object size over a certain age will trigger Survior area is 50%.

When are large objects recycled?

Large objects do not belong to the new generation and do not belong to the old generation, so in the new generation or the old generation collection will be accompanied by the collection of large objects’ region memory. This means that the collection of large objects relies on the Mixed collection.

Other Information:

Algorithm details excerpt

Some of my notes in the book are not suitable for the text, so I put them at the end (no one reads them anyway, haha).

Region uses two AMS Pointers to allocate a part of the Region’s space partition objects. All newly allocated objects exist in this interval. Similarly, if the collection fails to catch up with the memory allocation, the user thread should also be frozen to stop world.

The predictive model depends on: -XX: MaxgCrausemillion expected value, within which G1 will make the most efficient collection

Algorithm:

G1 uses decaying mean algorithm to calculate region collection time and dirty card data

  1. Reference values: 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 as fast as object allocation

Dry goods article:

Zhihu: G1 collector principle understanding and analysis

Consult the principle of G1 algorithm

Meituan: Some of the key technologies of the Java Hotspot G1 GC

Write at the end:

In the end, the JVM is really hard. The book “Deep Understanding of the JVM Virtual Machine” can only give you a rough idea of the general principles of the current mainstream collector implementation. If you want to dig deeper, it will take a long time to accumulate, and of course we don’t need to learn that much pain.

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.