JVM garbage collector (Part 1)-CMS collector | More challenges in August

To learn more about the G1 garbage collector, you need to take a look at the garbage collector before G1. If you don’t know more about the G1 garbage collector, click on the JVM garbage collector (part 1)-CMS Collector | More challenges in August

G1 collector

The G1(garbage-first) collector was introduced in JDK1.7 as a replacement for the CMS collector. It is an epoch-making Garbage collector that both inherits the CMS collector and lays the foundation for subsequent collectors such as ZGC. Is a garbage collector for servers, mainly for machines equipped with multi-core processors and large memory capacity. High throughput performance characteristics while meeting GC pause time requirements with high probability.

Firstly, in G1, the heap is no longer divided into fixed Eden zone,Survivol zone and old age zone according to the proportion as before. Although the G1 cancels the previous one

Instead, the heap is divided into independent regions of equal size, called regions. The maximum number of regions is 2048, which is 2 to the eleventh power. Regions are the smallest reclamation unit of G1. Although G1 cancelled the previous proportional division into fixed Eden zone,Survivol zone and old age zone. However, there are still concepts of Cenozoic era and old era, but they are not fixed regions. They can be discontinuous collections of regions, and regions will change. A Region may be a young generation now, but after several GC, it will become an old age Region, that is to say, regions will change dynamically.

The G1 has a few special features:

  • For example, regions of the young generation account for 5% of the heap memory by default. However, as the program continues to run,G1 adds more regions and expands the Region size. By default, the maximum size of regions is 60%.
  • Processing of large objects: Compared to previous collectors, G1 collector has more processing of large objects. It has a Region for storing large objects called Humongous Region. Instead of putting large objects directly into the old age, G1 collector determines that a large object exceeds the size of a Region by 5 0%, for example, if each Region is 2M, if an object exceeds 1M, it is judged as a large object and placed in Humongous. In addition, if a large object is too large, it may be stored across multiple regions. For example, a 4M large object is stored across two regions. Humongous area is specially used to store short-term giant objects, not directly into the old age, so as to save the space of the old age, avoid the old age space is not enough to produce Full GC to affect the performance of the program. In addition to collecting the young generation and the old generation, Humongous region will also be collected in Full GC.

G1 Region allocation diagram:

G1 Garbage Collection Steps The G1 collector garbage collection steps are roughly divided into four steps:

  • Initial tag: The initial tag is a fast phase that pauses other worker threads and records objects that GC Roots can reference directly.
  • Concurrency token: same as CMS concurrency token
  • Final marking: Similar to CMS final marking steps, The difference is The SATB(Snapshot At The Beginning) used in G1.
  • Filter collection: In the filter collection phase, the value and time of each Region are sorted. According to the expected GC pause time, the maximum level of regions is reclaimed within the pause time. For example, 100 regions are full in the old age. The garbage collection may pause for 100ms. According to the calculation of the previous collection cost, it may take 100ms to reclaim 80 regions, so only these 80 regions will be reclaimed to achieve the set GC pause time.

G1 Garbage Collection step diagram:

Region priority list the G1 collector maintains a priority list that stores the value of regions reclaimed in the background. Based on the pause time, the G1 collector preferentially selects the Region with the highest value to be reclaimed. For example, a Region can reclaim 10 MB of garbage in 10ms. The other Region can collect 20M garbage in 50ms. If the time for collecting garbage is limited, G1 preferentially selects the previous Region for collecting garbage. This collection ensures that the G1 collector can collect more efficiently in the shortest time.

Garbage collection algorithm: mark-tidy.

Use parameter Settings: -xx :+UseG1GC

Other parameter Settings:

  • -xx :G1HeapRegionSize: specifies the partition size (1MB to 32MB, the value must be the NTH power of 2). By default, the heap is divided into 2048 partitions

  • -xx :MaxGCPauseMillis: target pause time (default: 200ms)

  • -xx :G1NewSizePercent: Initial memory size of the new generation (default heap 5%)

  • -xx :G1MaxNewSizePercent: indicates the maximum memory size of the new generation

  • – XX: TargetSurvivorRatio: Survivor area filling capacity (50%) by default, Survivor in the area of a group of objects (age 2 + 1 + age age n) age objects, 50% of the total more than the Survivor area, At this point, objects older than n are placed in the old age

  • -xx :MaxTenuringThreshold: maximum age threshold (default 15)

Usage scenarios of G1:

  1. Live objects account for more than 50% of the heap memory

  2. The speed of object assignment and promotion varies greatly

  3. The GC time exceeds 1s

  4. More than 8GB of heap memory

  5. The expected pause time is relatively short, such as less than 500ms

Conclusion:

There is no doubt that the garbage recovery period of G1 is a very good, especially in JDK9 began, G1 is the default JVM garbage collector, but even the G1 is insufficient in its own place, such as though can support large memory, such as more than 8 g, alone on the terabytes of memory is not line, here have to recommend ZGC, interested partners can go Let’s solve for ZGC. From Serial to G1, garbage collectors are definitely getting stronger, with shorter memory and pause times, and maybe even auto-tuning as the program runs. Let’s see. Q.E.D.