Overview of G1 garbage collection cycle

Young-only phase: This phase starts with some ordinary Young objects and promotes them to the old age. The transition between the young generation and the space reclamation phase begins when the old generation occupancy reaches the initial heap occupancy threshold. At this point, G1 schedules a Concurrent Start young collection instead of a normal young collection.

  • **Concurrent Start ** : In addition to performing normal young object recycling, the marking process is also started. The concurrent tag determines all currently accessible (active) objects in the old-age region to be reserved for the subsequent space reclamation phase. When the collection marking is not completely complete, there may be instances where surviving objects are re-held references and must finally be marked.
  • Tag (STW) : This pause completes the tag itself, performs global reference processing and class unload, reclaims completely empty areas, and cleans up internal data structures. G1 computes the information between Remark and Cleanup so that it can later reclaim free space in the selected old-age region at the same time, which is done when Cleanup pauses.
  • Cleanup (STW) : This pause determines whether the space reclamation phase actually ensues. If the space reclamation stage is followed, only the young stage ends with a single Prepare Mixed young to be reclaimed.

Space reclamation phase: This phase consists of multiple mixed collections that, in addition to the young generation region, also clear the live objects of the old generation region set. The space reclamation phase ends when G1 determines that evacuating more old age areas will not produce enough available space.

Restart: After space is reclaimed, the collection cycle restarts from another only young phase, and G1 performs in-place Full GC (STW) like other collectors if the application runs out of memory while collecting activity information.

2. G1-oracle’s official recommendations

The general recommendation is to use G1 with its default Settings, eventually give it a different pause time target, and set the maximum Java heap size -xmx as needed. The balance of G1 defaults differs from other collectors. G1’s default configuration aims to balance neither maximum throughput nor minimum latency, but rather to provide relatively small, uniform pauses at high throughput. However, G1’s mechanism for incremental reclamation of space in the heap and its pause-time control impose some overhead in terms of application threads and space reclamation efficiency.

If you prefer high throughput: reduce GC pause times, or increase the size of the pair space.

  • Lower GC pause time: Set a smaller value-XX:MaxGCPauseMillis
  • Provides larger heaps to relax pause time objectives:-Xmx

Tips: Modify pause time goals if latency is the main requirement. Avoid using options such as -xmn and -xx :NewRatio to limit the young generation size to specific values, as the young generation size is the main means by which G1 allows it to satisfy pause times. Setting the young generation size to a single value overrides and effectively disables pause time control.

Iii. How to improve G1 performance

Full GC

A Full GC can be detected by finding _Pause Full (Allocation Failure)_ in the log. Full GC is usually preceded by garbage collection with evacuation failure indicated by the to-space Exhausted flag.

  • Increases the size of the heap space (-xx :G1HeapRegionSize) but increases the tagging time.

  • Explicitly increase the number of concurrent tagged threads (-xx :ConcGCThreads)

  • Force G1 to start marking earlier. G1 automatically determines the initial heap percentage (IHOP) threshold based on earlier application behavior. These predictions can be wrong if the application behavior changes. There are two options:

    • Display the garbage usage of Settings:-XX:G1ReservePercent
    • Added adaptive IHOP-XX:-G1UseAdaptiveIHOP. To override the G1 automatically calculates the value: – XX: InitiatingHeapOccupancyPercent

Huge object fragments

  • Program code reduces the number of large object allocations or increases heap space

Because you need to find a contiguic set of regions for them, Full GC may occur before all of the Java heap memory is exhausted. In this case, the potential choice is to increase the size of the heap region by increasing the option value -xx :G1HeapRegionSize to reduce the number of large objects or increase the size of the heap.

Abnormal GC operating system time & actual time

User=0.19s Sys=0.00s Real=0.01s. User time is the time spent on THE VM code, _ system time_ is the time spent in the operating system, and _real time_ is the absolute amount of time elapsed during pauses. If the system time is relatively long, it may be due to the VM environment configuration.

Common known problems with high system times are:

  • Dynamically scaling heap space causes unnecessary delays.

    • By using options-XmsAnd set the minimum and maximum heap sizes to the same values to avoid delays-XmxAnd use pre-touch all memory-XX:+AlwaysPreTouchTo move this work to the VM startup phase.
  • Because some background tasks intermittently occupy all the I/O bandwidth of the log writing disk, the log writing output may stop for a period of time. Consider using a separate disk, such as a memory-supported file system, for your logs or some other storage to avoid this.

  • In Linux in particular, merging small pages into large pages through the _ Transparent Large Page (THP)_ feature tends to cause random processes to stop, not just during pauses. Because the VM allocates and maintains a large amount of memory, there is a higher than usual risk that the VM will become a long-stalled process. Refer to your operating system documentation for information on how to disable transparent large pages.

Another situation to note is that the real-time time is much greater than the sum of the other cases, which may indicate that the VM is not getting enough CPU time on an overloaded machine.

Reference object processing takes too long

Object copying requires changes to RemberSet references. By default, G1 tries to use the following heuristic parallelizer stages for Reference Processing: One thread is started for each -xx :ReferencesPerThread reference object, limited by -XX:ParallelGCThreads.

  • By setting the-XX:ReferencesPerThread0 by default all available threads are used to disable this heuristic method,
  • Or disable parallelization altogether-XX:-ParallelRefProcEnabled.

The young-only phase takes too long

The time it takes the young generation to collect is roughly proportional to the size of the young generation, or to the number of live objects that need to be copied in the Collection Set.

  • To reduce-XX:G1NewSizePercentTo reduce the proportion of young generation in heap space.
  • Another problem with the size of the young generation can arise when the number of living objects in the collection suddenly changes. This can lead to a spike in garbage collection pause times. It may be useful to use a reduction in the maximum young generation size-XX:G1MaxNewSizePercent. This limits the maximum size of the young generation and the number of objects that need to be processed during pauses.

Mixed collection takes too long

  • Added value: -xx :G1MixedGCCountTarget Specifies the maximum number of MixedGC fires in a period. The default value is 8
  • Not to put them in the candidate set focus to avoid collecting area that need a lot of time to collect: XX: G1MixedGCLiveThresholdPercent below setting on the occasion of the region to return to the CSet, directly affect the Mixed GC select reclaimed areaUseLargePages
  • Increase the frequency of mixedGC: default 5%, decrease -xx :G1HeapWastePercent, but increase the frequency of mixedGC. Multiple mixedGCs are emitted when the specified value is exceeded

High frequency update RS and scan RS times

  • By reducing the size of the heap region: -xx :G1HeapRegionSize
  • Reduce the Rset pause time consuming: reduce the parameter value: – XX: G1RSetUpdatingPauseTimePercent

Adjusting throughput

G1’s default policy tries to strike a balance between throughput and latency; However, in some cases higher throughput is required. In addition to reducing the overall pause time as described in the previous sections, you can also reduce the frequency of pauses. The main idea is to increase the maximum pause time by using -xx :MaxGCPauseMillis.

The generation size automatically ADAPTS to the size of the young generation, which directly determines the frequency of pauses. If this does not result in the expected behavior, especially during the space reclamation phase, increasing the minimum young generation size -xx :G1NewSizePercent will force G1 to do so.

In some cases, the maximum allowable young generation size of -xx :G1MaxNewSizePercent may limit throughput by limiting the young generation size.

  • Attempts to reduce concurrent workload, especially concurrent memory set updates, often require a lot of CPU resources. increase-XX:G1RSetUpdatingPauseTimePercentMove work from concurrent operations to garbage collection pauses. In the worst case, concurrent memory set updates can be disabled by setting them-XX:-G1UseAdaptiveConcRefinement -XX:G1ConcRefinementGreenZone=``2G -XX:G1ConcRefinementThreads=``0. This essentially disables this mechanism and moves all remembered collection updates to the next garbage collection pause.
  • Enable the use of large pages-XX:+UseLargePagesThroughput can also be improved.

Turn – note | accept – hidden | – who are below, are you sure don’t three even for a moment?