Because love so insist, because love so wait. Through the long days without drama to play, finally for the spring of life, mutual encouragement!!

1. What is the G1 garbage collector?

①. G1(garbage-First) is a Garbage collector for server-side applications. It is mainly aimed at machines equipped with multi-core CPUS and large memory capacity

In JDK1.7, it is the default garbage collector after JDK 9, replacing the CMS collector.

2. Why is it called Garbage First?

①. G1 is a parallel collector that divides the heap memory into many unrelated regions (regions that are physically discontinuous) and divides the heap into 2048 regions, each of which is 1-32M in size and must be an integer power of 2. Different regions can be used to represent Eden, Survivor 0, Survivor 1, and the old age

②. The Region with the greatest value is reclaimed first according to the allowed collection time. (After each collection, an idle Region is reclaimed and a priority list is maintained in the background.)

③ Since this method focuses on the Region with the largest amount of Garbage collected, we gave G1 the name Garbage First.

④ Why do we publish Garbage First(G1) GCS when we already have the First few powerful GCS?

  • The official goal for the G1 is to achieve the highest throughput possible with manageable latency, hence the heavy burden and expectation of a “fully functional collector.”

2. Divide Region: Divide the whole into parts

  • When using the G1 collector, it divides the entire Java heap into approximately 2048 independent regions of the same size, each Region size depending on the actual size of the heap, and the overall Region size is controlled between 1MB and 32MB to the NTH power of 2. That’s 1MB,2MB, 4MB, 8MB, 16MB, 32MB. -xx :G1HeapRegionSize Can be set. All regions are the same size and do not change during the lifetime of the JVM.
  • Although the concept of Cenozoic and oldyn is still retained, Cenozoic and oldyn are no longer physically separated; they are collections of parts of regions (which do not need to be continuous). Dynamic Region allocation is used to achieve logical continuity.

  • A region may belong to Eden, Survivor, or 0LD /Tenured memory regions. However, a region can belong to only one role. In the figure, E indicates that the region belongs to Eden memory region, S indicates that the region belongs to Survivor memory region, and O indicates that the region belongs to old memory region. Blank Spaces in the figure represent unused memory space.
  • The G1 garbage collector also adds a new memory region called the Humongous memory region, shown in block H. It is used to store large objects. If the number of regions exceeds 1.5, it is placed in H.
  • The reason for setting H:

Large objects in the heap are directly assigned to the old age by default, but if it is a short-lived large object, this can have a negative impact on the garbage collector. To solve this problem, G1 has a Humongous section, which is dedicated to large objects. If an H block does not fit a large object, G1 looks for contiguous H blocks to store. Sometimes you have to start the Full GC in order to find consecutive H regions. Most of G1’s behavior treats the H region as part of the old age.

3. Features and disadvantages of the G1 garbage collector

Parallelism and concurrency

  • Parallelism: G1 can have multiple GC threads working at the same time during collection, effectively leveraging multi-core computing power. At this point the user thread is STW
  • Concurrency: G1 has the ability to alternate execution with the application, so that some work can be performed at the same time as the application, so that, generally speaking, the application does not completely block during the entire reclamation phase

②. Collection by generation

  • In terms of generation,G1 is still a generational garbage collector. It differentiates the young generation from the old generation, and the young generation still has Eden and Survivor zones. However, from the structure of the heap, it does not require the whole Eden area, the young generation or the old generation to be continuous, nor does it insist on fixed size and fixed quantity.
  • The heap space is divided into regions that contain logical young and old generations.
  • Unlike all the previous types of recyclers, itBoth the younger generation and the older generation. Compare other recyclers, either working in the younger generation or working in the older generation

③ spatial integration

  • The G1 divides memory into regions. Memory reclamation is based on region. Region to Region is a copy algorithm, but overall can actually be regarded as a Mark to Compact algorithm, both algorithms can avoid memory fragmentation. This feature helps programs run for a long time and allocate large objects without triggering the next GC prematurely because contiguity memory space cannot be found. This is especially true when the Java heap is very large.)

This is another big advantage of G1 compared with CMS. In addition to the pursuit of low pause,G1 can also establish a predictable pause time model, allowing users to clearly specify a length of M ** -xx :MaxGCPauseMillis**

  • Due to partitioning,G1 can select only part of the region for memory reclamation, which reduces the scope of reclamation, so that the occurrence of global pause can be well controlled
  • G1 tracks the value of garbage accumulation in each Region (the amount of garbage collection space obtained and the experience value of garbage collection time), maintains a priority list in the background, and collects garbage from the Region with the highest value according to the allowed collection time. The G1 collector is guaranteed to achieve the highest possible collection efficiency in a limited time.
  • G1 is not necessarily as good at delaying pauses as CMS GC is at best, but much better at worst

5. Disadvantages:

  • Compared to CMS,G1 does not have a comprehensive, overwhelming advantage. For example,G1 has a higher garbage collection Footprint and Overload than CMS during user program execution.
  • Empirically, CMS is more likely to outperform G1 in small memory applications, while G1 is more likely to outperform G1 in large memory applications. The balance point is between 6-8GB

4. Set parameters

** -xx :+UseG1GC:** Manually specify the use of G1 collector to perform memory collection tasks.

②. -xx :G1HeapRegionSize: Set the size of each Region. The value is a power of 2, ranging from 1MB to 32MB, and the goal is to partition about 2048 regions based on the minimum Java heap size. The default is 1/2000 of the heap

③. -xx :MaxGCPauseMillis: Sets the maximum GC pause time that the JVM will try to achieve, but not guarantee. The default is 200ms (if this is set to a very small value, such as 20ms, it will collect fewer regions, and after a long time, the heap will be full. FullGC generates STW, which affects user experience.

** -xx :ParallelGCThread:** Set the value of the number of GC threads in STW. Maximum set to 8(garbage collection threads)

** -xx :ConcGCThreads:** Sets the number of concurrent threads to be tagged. Set n to about 1/4 of the number of parallel garbage collection threads (ParallelGCThreads)

6. * * – XX: InitiatingHeapOccupancyPercent: * * set the trigger a concurrent GC cycle Java heap usage rate threshold value. If this value is exceeded, GC is triggered. The default value is 45

5. Usage scenarios of G1

  • Server – oriented applications for machines with large memory and multiple processors. (Does not work in a normal-sized heap

Surprise), the most important applications are those that require low GC latency and have a large number of applications to provide solutions

For example, when the heap size is about 6GB or larger, predictable pause times can be less than 0.5 seconds; (G1 ensures that GC pauses are not too long by incrementally cleaning only one Region at a time instead of all.)

  • To replace the CMS collector in JDK1.5

Using G1 may be better than CMS when: ① More than 50% of the Java heap is occupied by active data; (2) The frequency of object assignment or chronological lifting varies greatly; ③ The GC pause time is too long (longer than 0.5 to 1 second).

  • In addition to G1, other garbage collectors use a built-in JVM thread to perform multi-threaded GC operations. G1 GC can use application threads to perform background GC operations. When the JVM’s GC thread is slow, the application thread is called to help speed up the garbage collection process.

6. G1 collector garbage collection process

①. The garbage recovery process of G1 GC mainly includes the following three steps:

  • Young GC
  • Concurrent Marking in the old days
  • Mixed GC

(Single-threaded, exclusive, high-intensity Full GC will still exist if needed. It provides a fail-safe mechanism against GC evaluation failures, namely strong collection.

Clockwise young GC -> Young GC + Concurrent mark-> Mixed GC

②. The application allocates memory, and the process of young generation reclamation begins when the Eden area of the young generation is used up; G1’s young-generation collection phase is a parallel (multiple garbage threads) exclusive collector. During the young generation collection period, the G1 GC suspends all application threads and starts multithreading to perform the young generation collection. Then move the surviving object from the young generation to the Survivor or the old, or possibly both

③ When heap memory usage reaches a certain value (45% by default), the old-age concurrent marking process begins

Start the mixed recycling process as soon as the mark is finished. For a mixed payback period, the G1 GC moves live objects from the old period to the free period, which becomes part of the old period. Unlike the young generation, the G1 collector of the old generation does not need to recycle the entire old generation, but only scan/reclaim a small number of old regions at a time. At the same time, the old Region is reclaimed along with the young generation.

For example, for a Web server, the Java process has a maximum heap memory of 4G, responds to 1500 requests per minute, and allocates about 2G of memory every 45 seconds. G1 does a young generation collection every 45 seconds, with a heap utilization rate of 45% every 31 hours, and begins the old generation concurrent tagging process, followed by four or five mixed collections

Memory sets and write barriers

Object referenced by different regions:

  • A Region cannot be isolated. Objects in a Region may be referenced by objects in any Region. For example, when a generation reference an old age, the STW is scanned during garbage collection
  • Do I need to scan the entire Java heap to be accurate when determining whether an object is alive?
  • In other generational collectors, this is also a problem, right? (The G1 is more prominent.)
  • Will the new generation also have to scan the old?
  • This would reduce MinorGC’s efficiency

②. Solutions:

  • Both G1 and the strip collector, the JVM uses Remembered Set to avoid global scans. Each Region has a corresponding Remembered Set
  • Each time Reference data is written, a Write Barrier is created
  • Then check whether the Reference to be written refers to an object in a different Region than the Reference type.
  • If not, the related references are recorded in the Remembered Set of the Region where the reference points to the object through CardTable.
  • When garbage collection is performed, add the enumeration scope of the GC root to Remembered Set. You can guarantee that no global scan will be done, and there will be no omissions

Details of G1 recovery

G1 recovery process 1: young generation GC

Recovery time (1). When Eden space runs out,G1 will start a garbage collection process of the young generation (2). Young generation garbage collection will only collect Eden and Survivor areas (3). Before recycling:

(4) After recycling:



Stage 1, root scan:

  • Be sure to consider remembered Set to see if any objects of the old age reference objects of the new generation
  • The root refers to the object to which the static variable points, the local variable in the chain of method calls being executed, and so on. The root reference along with the external reference of the RSet record serves as the entry point for scanning the living object.

Phase 2, update RSet:

  • Process cards in the Dirty Card queue(see remarks) and update the RSet. After this phase is complete, the RSet can accurately reflect the reference of the old age to the object in the memory segment

Dirty Card Queue: For application reference assignment statements object.field=object, the JVM performs special operations before and after to enqueue a card that holds object references in the dirty Card queue. During the rollback of the young generation,G1 will process all cards in the Dirty CardQueue to update the RSet and ensure that the RSet accurately reflects the reference relationship in real time. Why not update the RSet directly at the reference assignment statement? This is for the sake of performance,RSet processing requires thread synchronization, which can be very expensive, using queue performance is much better

Stage 3, processing RSet:

  • Identify the objects in Eden that are pointed to by the old objects. The objects in Eden that are pointed to are considered alive

Stage 4, Copy objects:

  • At this stage, the object tree is traversed, and the surviving objects in the memory segment of Eden area will be copied to the hollow memory segment of Survivor area. If the age of surviving objects in the memory segment of Survivor area does not reach the threshold, the age will be increased by 1. When the age reaches the threshold, the surviving objects will be copied to the hollow memory segment of old area. If Survivor space is insufficient, some data in Eden space will be promoted directly to the old space

Stage 5, handling references:

Handle Soft,Weak, Phantom, Final, JNI Weak etc references. Finally, the data in Eden space is empty,GC stops working, and the objects in the target memory are continuously stored without fragmentation. Therefore, the replication process can achieve the effect of memory consolidation and reduce fragmentation

②. Recycling process two: the old concurrent marking process

I. Initial marking stage:

  • Marks objects directly reachable from the root node. This phase is STW and triggers a young GC

Root Region Scanning

  • The G1 GC scans the Survivor zone ** directly reachable old age zone objects,** and marks the referenced objects. This process must be completed before the YoungGC (with YoungGC, Survivor zones will be touched, so this process must be completed before the YoungGC)

Iii. Concurrent Marking:

  • Concurrent marking (and application concurrent execution) throughout the heap may be interrupted by the Young GC. During the concurrent marking phase, if all objects in a region object are found to be garbage, the region is immediately reclaimed. At the same time, the object activity (the percentage of living objects in the region) of each region is calculated during concurrent tagging.

Remark:

  • As the application continues, you need to fix the last marked result. Is the STW. G1 uses a faster initial snapshot algorithm than CMS: Snapshot one at one the Beginning (SATB).

Cleanup (STW):

  • Calculate the ratio of live objects and GC collection for each region and sort them to identify regions that can be mixed for collection. Set the stage for the next phase. Is the STW. (This phase will not actually do garbage collection)

Vi. Concurrent cleaning stage:

  • Identify and clean areas that are completely free

③. Collect Mixed GC

Mixed GC is not FullGC, the heap of Old s share reach parameters (- XX: InitiatingHeapOccupancyPercent) set the value of the trigger, recycle all part of Young and Old (according to expect GC pause time determine the priority of the Old area garbage collection) and the large object area, is In most cases, G1 garbage collection is MixedGC first, mainly using the replication algorithm. The surviving objects in each region need to be copied to other regions. During the copying process, if there are not enough empty regions that can carry the copied objects, Full GC will be triggered

  • After the concurrent marking ends, the segments that are 100% garbage in the old age are reclaimed and the segments that are partially garbage are calculated. By default, these older memory segments are collected eight times (which can be set to -xx :G1MixedGCCountTarget).
  • The Collection Set of a mixed Collection consists of one-eighth of old age segments, Eden segment, and Survivor segment. The algorithm of hybrid collection is exactly the same as the algorithm of young generation collection, but it collects more memory segments of the old generation. Please refer to the young generation recycling process above for details.
  • Since memory segments are recycled eight times by default in older generations, G1 prioritises memory segments with more garbage. The higher the percentage of garbage in memory segments, the more garbage will be collected first. And has a threshold value will determine whether memory segments are recycled, – XX: G1MixedGCLiveThresholdPercent, the default is 65%, mean waste of memory block to achieve 65% can be recycled. If the garbage ratio is too low, it means that there is a high percentage of live objects, which will take more time to replicate.
  • Mixed recycling does not have to be done eight times. There is a threshold ** -xx :G1HeapWastePercent**, which defaults to 10%, meaning that 10% of the total heap memory is allowed to be wasted, meaning that if the percentage of garbage that can be recycled is less than 10% of the heap memory, no mixed recycling is done. Because GC takes a lot of time but recycles very little memory.

④. Optional process 4 of G1: Full GC

  • The G1 was designed to avoid Fu1l GC. But if that doesn’t work, G1 stops The application’s execution (stop-the-world) and uses a single-threaded memory reclamation algorithm for garbage collection, with poor performance and long application pauses.
  • To avoid Full GC, you need to adjust once it happens. When will Full GC happen? For example, if the heap is too small, G1 will fall back to full GC when there is no empty segment available for copying live objects, a situation that can be resolved by increasing memory.
  • There are two possible causes of G1Full GC:

1. There is not enough to-space to store promoted objects when recycling

2. Space runs out before the concurrent processing is complete

7. Optimization suggestions for G1 collector

① The size of the younger generation

  • Avoid using options such as -xmn or -xx :NeyvRatio to explicitly set the size of the young generation
  • Fixed the size of the young generation to override the pause time target

② Don’t be too strict with your pause time goals

  • The throughput goal for the G1 GC is 90% application time and 10% garbage collection time
  • When evaluating G1 GC throughput, don’t be too harsh with pause time goals. Being too stringent means you are willing to incur more garbage collection overhead, which directly affects throughput.

Reference video: Silicon Valley JVM complete tutorial, millions of playback, the peak of the network (Song Hongkang details Java VIRTUAL machine) reference books: in-depth understanding of the Java virtual machine