CMS and G1 Collectors: Low-Latency Garbage Collection

Understand G1 for current HotSpot deployments and CMS as a historical collector removed in JDK 14, with version-labeled examples and GC logging.

published: reading time: 25 min read author: Geek Workbench
Quick Summary

Understand G1 for current HotSpot deployments and CMS as a historical collector removed in JDK 14, with version-labeled examples and GC logging.

CMS and G1 Collectors: Low-Latency Garbage Collection

G1 is the default collector in HotSpot JDK 9 and later, but CMS remains useful to understand when maintaining older Java deployments or reading historical tuning guides. Both collectors perform some work concurrently with application threads, which can reduce pause times at the cost of CPU and implementation complexity.

This guide explains G1 for current HotSpot deployments, then uses CMS as historical migration context. Collector behavior and available flags depend on the JDK build; check the documentation for the exact vendor and release you run.

Introduction

G1 is a mostly concurrent collector in current HotSpot releases. CMS is a historical collector that was deprecated in JDK 9 and removed in JDK 14; it is unavailable on modern JDKs. G1 became the default in JDK 9. These are HotSpot release facts, not a promise that every vendor build exposes identical options. For current behavior, see Oracle’s Java 21 G1 tuning guide and use your JDK vendor’s matching documentation.

G1 divides the heap into regions and chooses collections using its pause-time prediction model. Its pause target is a goal, not a guarantee, and it still performs stop-the-world pauses. The CMS section below explains the older mark-sweep design and its fragmentation and fallback behavior; it is not a choice for JDK 14 or later. See JEP 363 for the removal and Oracle’s JDK 21 GC tuning guide for currently documented HotSpot collectors.

When to Use This Knowledge

Use this guide when comparing G1 tuning with older CMS guidance or diagnosing a JDK 8 HotSpot deployment. For a current deployment, first check which collectors the exact runtime supports, then measure with representative load and latency targets.

When NOT to Use This Knowledge

If your production environment runs ZGC or Shenandoah, most CMS and G1 tuning advice does not apply. These collectors work differently and require different approaches.

For a current deployment with a tight latency target, compare the collectors available in your exact JDK and vendor build, including G1, ZGC, or Shenandoah where supported. Measure application latency and throughput under representative load before choosing; no collector removes the need to validate pause behavior.

CMS was deprecated in JDK 9 and removed in JDK 14. Its flags and failure modes matter when diagnosing or migrating older HotSpot deployments, not when configuring a modern JDK. For current options, begin with the collector documentation for your JDK vendor and version. For related JVM topics, see the Advanced Java & JVM Internals roadmap.

The Concurrent Approach

CMS and G1 perform some collection work concurrently with application threads. Both still use stop-the-world pauses, and concurrent work competes with the application for CPU.

graph TB
    subgraph Serial["Serial/Parallel GC"]
        direction TB
        A1["Stop The World"] --> A2["GC Work"] --> A3["Stop The World"] --> A4["GC Work"]
    end
    subgraph Concurrent["CMS / G1"]
        direction TB
        B1["Initial Mark\n(STW - short)"] --> B2["Concurrent Mark"] --> B3["Remark\n(STW - short)"] --> B4["Concurrent Sweep"] --> B5["Concurrent Sweep"]
    end

Concurrent work can reduce some pauses, but it uses CPU that would otherwise be available to the application. Measure both pause behavior and throughput.

G1 Collector (current HotSpot example: JDK 21)

G1 became the default collector in HotSpot JDK 9. It divides the heap into equal-sized regions rather than treating the old generation as one contiguous space.

graph TB
    subgraph G1Heap["G1 Heap - Divided into Regions"]
        direction TB
        R1["Region 1\n(Eden)"]
        R2["Region 2\n(Survivor)"]
        R3["Region 3\n(Old)"]
        R4["Region 4\n(Old)"]
        R5["Region 5\n(Eden)"]
        R6["Region 6\n(Humongous)"]
    end

How G1 Works

G1 divides the heap into regions of equal size (typically 1MB to 32MB depending on heap size). Each region can be Eden, Survivor, or Old generation - the designation is fluid and changes between GC cycles.

G1 tracks live object density per region. During a GC, it collects regions with the most garbage first - hence “Garbage First.”

graph TB
    subgraph G1Cycles["G1 GC Cycles"]
        A["Young GC\n(Copying - STW)"] --> B["Concurrent Mark\n(regions)"]
        B --> C["Mixed GC\n(Evacuate old regions - STW)"]
        C --> A
    end

G1 GC Phases

  1. Young GC (Stop-The-World): Copy live objects from young regions to survivor regions or promotes to old regions. Uses multiple threads.

  2. Concurrent Marking: Like CMS, G1 performs concurrent marking to identify live objects across all regions. Runs in background threads.

  3. Remark (Stop-The-World): Like CMS, finalizes marking of objects modified during concurrent phase.

  4. Mixed GC: After concurrent marking, G1 may collect a mix of young and old regions. G1 selects regions with lowest live ratio first.

  5. Cleanup: Frees completely empty regions, updates remembered sets.

Why G1 Replaced CMS

Aspect CMS G1
Heap division Contiguous Regions
Compaction None by default (fragmentation) Incremental (per-region)
Full GC Falls back to Serial/Parallel G1 handles it
JDK support Removed in JDK 14 Default in HotSpot since JDK 9
Fragmentation No compaction during concurrent cycle Region-based evacuation; workload dependent
Predictability Poor (no pause target) Good (pause time target configurable)

G1’s region-based design lets it compact incrementally - it does not need to compact the entire old generation at once. This makes pause times more predictable and manageable.

Pause Time Target

G1 lets you set a target for maximum GC pause time:

-XX:MaxGCPauseMillis=200    # Target 200ms max pause

G1 then tunes its collection to try to meet this target. Note: this is a target, not a guarantee. Under memory pressure, G1 may not be able to meet it.

G1 estimates the work for candidate regions using prior collection data and live-object estimates. MaxGCPauseMillis is a goal used by the collector’s prediction model, not a pause guarantee. The collection set and actual pause depend on the heap, workload, and runtime conditions. See the Java 21 G1 guide.

When G1 cannot meet the target, allocation pressure has outpaced its ability to collect fast enough. G1 still triggers a young GC to avoid heap exhaustion, but the pause overshoots the target. The next cycle tries to collect more regions. If pauses keep exceeding the target, you can increase heap size, relax the target, or reduce garbage production by sizing the young generation down.

An aggressive MaxGCPauseMillis target can increase collection frequency or GC CPU without keeping every pause below the target. Choose a goal from the service latency budget, then measure pause distribution, throughput, and GC CPU under representative load.

Key Characteristics of G1

Aspect Behavior
GC threads Multiple + concurrent
Pause time Configurable target via MaxGCPauseMillis
Throughput Lower than Parallel GC (concurrent phases use CPU)
Heap layout Region-based (1MB to 32MB regions)
Compaction Incremental per-region
Young gen Dynamic - size adjusts between Min and Max
Large objects Humongous regions handle objects > 50% of region size

JVM Flags for G1

# HotSpot JDK 21 example; verify flags against your vendor and release.
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200    # Target, not a pause-time guarantee
-Xlog:gc*                 # Unified GC logging (JDK 9+)

CMS: legacy migration context

CMS (Concurrent Mark Sweep) was a HotSpot collector for older deployments. It was deprecated in JDK 9 and removed in JDK 14. The flags and examples in this section are legacy HotSpot material; they do not work on JDK 14 or later. Vendor builds and backports may differ, so check the exact runtime before applying migration advice.

How CMS Works

CMS phases:

  1. Initial Mark (Stop-The-World, short): Mark reachable objects from GC roots. Its duration depends on heap size, root set, hardware, and runtime conditions; inspect the GC log for your workload.

  2. Concurrent Mark: Traverse the object graph while application runs. Takes time but does not stop the world.

  3. Remark (Stop-The-World, short): Finalize marking of objects modified during concurrent phase (the “floating garbage” problem).

  4. Concurrent Sweep: Reclaim unmarked objects while application runs.

  5. Concurrent Reset: Prepare CMS data structures for next cycle.

graph TB
    A["Initial Mark\n(STW)"] --> B["Concurrent Mark\n(app running)"]
    B --> C["Remark\n(STW)"]
    C --> D["Concurrent Sweep\n(app running)"]
    D --> E["Concurrent Reset"]
    E --> A

Floating Garbage

CMS concurrent marking is imprecise. Objects that become unreachable during concurrent marking (modified by application threads) may not be reclaimed - they are called “floating garbage.” This garbage sits in the heap until the next CMS cycle.

This is a fundamental limitation of concurrent marking: you cannot stop the application while doing the full mark, so some objects slip through.

CMS accumulates floating garbage because of how its two-phase marking works. Initial mark grabs everything reachable at the start. Then concurrent mark walks the object graph while your app runs. During that walk, your app threads change object references — objects can slip from reachable to unreachable — but concurrent mark does not see those changes. The remark phase catches some of them, but not all. That gap is the floating garbage window.

Whether it matters depends on your allocation rate. The effect depends on allocation rate and available heap headroom. Under enough allocation pressure, floating garbage can contribute to old-generation exhaustion before a CMS cycle finishes. You can shrink the window by lowering -XX:CMSInitiatingOccupancyFraction so CMS kicks in earlier, but that costs more CPU.

G1 takes a different approach. Its snapshot-at-the-beginning (SATB) algorithm marks objects that were live when marking started, treating anything modified during marking as live even if it would have become unreachable. This is faster but leaves more floating garbage behind. G1 makes up for it through cleanup phases that reclaim regions with high garbage density, so the heap does not fill up as fast.

Key Characteristics

Aspect Behavior
GC threads Multiple + concurrent
Pause time Short pauses for Initial Mark and Remark
Throughput Lower than Parallel - concurrent phases use CPU
Old gen collection Mostly concurrent
Young gen collection Serial or Parallel Copying
Heap fragmentation Moderate - no compaction by default
Lifecycle Deprecated in JDK 9; removed in JDK 14

CMS Failure Modes

CMS can fail in two main ways:

  1. Concurrent Mode Failure: CMS cannot finish collecting before old generation fills up. Triggers a full stop-the-world GC as fallback.

  2. Promotion Failure: Object in young generation cannot fit in old generation during minor GC, triggering full GC.

Both failures come down to memory pressure, but the mechanism differs. Concurrent mode failure is a slow burn — CMS falling behind over multiple cycles. Promotion failure is an acute event during a specific minor GC. If you are hitting concurrent mode failure, lower -XX:CMSInitiatingOccupancyFraction or add more heap. If promotion is failing, increase old generation reservations or shrink the young generation to reduce individual promotion sizes.

Legacy HotSpot flags for CMS (JDK 8 examples)

-XX:+UseConcMarkSweepGC
-XX:ParallelCMSThreads=N     # Set only after checking the JDK 8 vendor documentation
-XX:CMSInitiatingOccupancyFraction=70  # Illustrative threshold; not a recommended default
-XX:+UseCMSInitiatingOccupancyOnly   # Use the configured threshold rather than adaptive pacing

These are historical HotSpot options for a JDK 8 deployment, not settings for a current JDK. In Oracle’s Java 8 documentation, the default initiating-occupancy threshold is described as approximately 92%, subject to release and collector ergonomics; 70 above is an example override, not the default. A lower threshold starts marking earlier and can increase concurrent GC work. Use the matching vendor documentation and logs before changing it. See the Oracle Java 8 CMS guide.

CMS does not compact during its concurrent sweep, so fragmentation could lead to a later full collection. The legacy CMS compaction flags and defaults vary across HotSpot releases; consult the matching Java 8 vendor documentation before relying on them.

Production Failure Scenarios

1. G1 Evacuation Failure

Symptom: GC pause for evacuation failure or to-space exhausted. Application freezes.

Cause: G1 ran out of free regions to copy survivor objects into. Happens when the to-space (target regions) fills up during a young or mixed GC.

Solution:

# Increase reserve space
-XX:G1ReservePercent=15

# Increase heap if possible
-Xms4g -Xmx4g

# Reduce expected pause time if target is too aggressive
-XX:MaxGCPauseMillis=500

2. CMS Concurrent Mode Failure

Symptom: CMS cannot complete before old generation fills. Full GC kicks in and freezes application.

Cause: Old generation filling faster than CMS can reclaim it. Usually means you need more heap, lower CMSInitiatingOccupancyFraction, or a different collector.

Solution:

# Start CMS earlier
-XX:CMSInitiatingOccupancyFraction=60

# Or just switch to G1
-XX:+UseG1GC

3. Humongous Object Issues (G1)

Symptom: Frequent long pauses with many humongous allocations.

Cause: Objects larger than half a G1 region (e.g., > 16MB with 32MB regions) are treated as “humongous” and collected differently. They can cause fragmentation in the humongous region space.

Solution:

# Increase region size if heap is large
-XX:G1HeapRegionSize=32m

# Avoid allocating very large buffers in hot paths

Trade-off Table

Configuration When to use Trade-off
-XX:+UseConcMarkSweepGC Legacy systems, Java 8 with low-latency needs Deprecated, no compaction, can fail badly
-XX:+UseG1GC Default for most low-latency workloads More CPU overhead than Parallel GC
-XX:MaxGCPauseMillis=100 Strict latency SLA May reduce throughput if too aggressive
-XX:G1HeapRegionSize=16m Large heaps (>16GB) Smaller regions = more regions to manage
-XX:CMSInitiatingOccupancyFraction=70 Tune CMS trigger point Too low = premature collections, too high = failure

Implementation Snippets

Checking Which Collector Is Running

import java.lang.management.*;
import java.util.*;

public class CollectorCheck {
    public static void main(String[] args) {
        List<GarbageCollectorMXBean> gcs = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gc : gcs) {
            System.out.println("Collector: " + gc.getName());
            System.out.println("  Cycles: " + gc.getCollectionCount());
            System.out.println("  Time: " + gc.getCollectionTime() + "ms");
        }
    }
}

Enabling G1 and Setting Pause Target

java -XX:+UseG1GC \
     -XX:MaxGCPauseMillis=150 \
     -XX:G1HeapRegionSize=16m \
     -XX:InitiatingHeapOccupancyPercent=45 \
     -Xms4g -Xmx4g \
     -Xlog:gc*:file=g1.log \
     -jar myapp.jar

Reading G1 GC Logs

G1 logs break down pause time by phase, making it easier to identify bottlenecks. Each pause entry tells you which type of GC ran, how much memory moved, and how long each sub-phase took.

[GC pause (G1 Evacuation Pause) 256M->245M(4G) 148.734ms]
[Ext Root Scanning: 23.4ms]
[Update RS: 12.1ms]
[Scan RS: 8.3ms]
[Object Copy: 89.2ms]

Each field in a pause log entry maps to something specific:

  • G1 Evacuation Pause — Young or mixed GC where G1 copies live objects from one set of regions to another. This is a stop-the-world event.
  • 256M->245M(4G) — Heap usage before and after the pause, and total heap size. In this case 256MB in use before, 245MB after, across a 4GB heap.
  • 148.734ms — Total pause duration. Compare this against your -XX:MaxGCPauseMillis target.

The indented sub-lines are phase timings:

Phase What it does
Ext Root Scanning Scans GC roots (thread stacks, object headers, JNI references)
Update RS Updates remembered sets for regions whose references changed
Scan RS Scans remembered sets for references pointing into the CSet
Object Copy Copies live objects from source regions to target regions

The phase timings add up to the total pause time. If Object Copy dominates, your young generation is too large or survival rate is high — G1 is spending most of its time moving objects. If Ext Root Scanning dominates, you have many threads with deep stacks or a lot of JNI code.

Other common log entries to watch for:

[Concurrent Mark]
[GC pause (G1 Humongous Allocation)]
[GC pause (G1 Cleanup)]
[Full GC]

Concurrent Mark runs in the background, no pause. Humongous Allocation pauses are brief but frequent ones point to large buffers being created in your hot path. Cleanup has a stop-the-world component for resetting region tables. Full GC means G1 fell back to serial compaction, which can take seconds on large heaps.

Use -Xlog:gc*=debug for full phase breakdowns during development, then switch to -Xlog:gc*:file=g1.log in production to capture enough detail without overwhelming log storage.

Observability Checklist

  • Enable GC logging: -Xlog:gc*:file=g1.log or -Xlog:gc*:file=cms.log
  • Monitor pause times from GC logs - look for pauses exceeding your target
  • Track jstat -gc <pid> for OC (old gen capacity) vs OU (old gen used)
  • Watch for evacuation failures in G1 logs: to-space exhausted
  • Monitor CMS concurrent mode failure events
  • For G1: check G1HeapRegionSize is appropriate for your heap size
  • Use -XX:PrintGCDetails for phase-level timing breakdown

Security Notes

  1. Concurrent phases use more CPU, which can reveal allocation patterns through timing side channels
  2. GC logs expose memory usage patterns and object lifetime characteristics
  3. JMX access to GC beans should be restricted - can reveal collector internals and tuning
  4. Large heaps mean larger heap dumps on OOM - more data to protect

Common Pitfalls / Anti-Patterns

Pitfall What happens Fix
Setting pause target too low G1 uses too much CPU trying to meet target Increase MaxGCPauseMillis or add more heap
Using CMS on JDK 14+ CMS is absent Select a supported collector
Ignoring G1 region size Suboptimal performance on large heaps Set G1HeapRegionSize explicitly
Too small G1ReservePercent Evacuation failures Increase to 15-20
Setting old gen threshold too high (CMS) Concurrent mode failure Lower CMSInitiatingOccupancyFraction

Quick Recap Checklist

  • CMS = legacy HotSpot mark-sweep collector; deprecated in JDK 9 and removed in JDK 14
  • G1 = region-based, incremental compaction, default since Java 9
  • Both do work concurrently to reduce pause times
  • Concurrent phases use CPU that could go to your application
  • G1 pause target set via -XX:MaxGCPauseMillis=N
  • G1 divides heap into 1MB-32MB regions
  • CMS fallback: concurrent mode failure triggers full stop-the-world
  • For JDK 14+, CMS is removed; select a collector supported by your runtime

Interview Questions

1. What is the fundamental difference between CMS and G1?

CMS treats the old generation as one contiguous space and performs mostly-concurrent marking and sweeping without compaction. G1 divides the entire heap into regions and performs incremental compaction - it compacts old generation region by region rather than all at once. This makes G1's pause planning and region-based evacuation address some fragmentation pressures, but the pause target remains a goal rather than a guarantee.

2. What causes concurrent mode failure in CMS?

CMS cannot finish its concurrent marking and sweeping before the old generation fills up. When this happens, the JVM falls back to a full stop-the-world GC (typically Serial or Parallel). This is usually caused by the occupancy threshold being set too high, too many allocations requiring promotion, or the concurrent phases running too slowly relative to allocation rate. Fix by lowering -XX:CMSInitiatingOccupancyFraction or switching to G1.

3. How does G1 achieve its pause time target?

G1 tracks the amount of work needed to collect each region and estimates how long it will take. It builds a collection set of regions to collect, ordered by garbage density (garbage-first). It stops adding regions to the set once it estimates the collection will exceed the pause target. This means under memory pressure, G1 may not be able to collect enough regions and may not meet the target - it is a soft goal, not a guarantee.

4. What are humongous regions in G1?

Objects larger than half a G1 region size go into humongous regions. A 64MB region holds objects up to 32MB; anything bigger gets its own humongous region. These objects are collected during the concurrent marking cycle and cleanup, not during normal young or mixed GC. The catch is that large humongous objects can fragment the humongous region space - a known rough edge of G1.

5. Why was CMS deprecated and replaced by G1?

CMS could suffer from fragmentation because its concurrent cycle did not compact the heap; a full collection could be needed when free space was insufficient. G1 introduced region-based collection and became the default in HotSpot JDK 9. Collector choice and pause behavior still depend on workload and runtime configuration.

6. What is the collection set in G1?

The collection set (CSet) is the set of regions G1 has chosen to collect during a GC cycle. G1 selects regions with the highest garbage density (most free space relative to live objects) first - hence "Garbage First." Young GC includes young regions; mixed GC includes both young and old regions. The CSet is built by estimating work per region and stopping when the pause target would be exceeded.

7. How does G1's incremental compaction differ from full compaction?

Full compaction (like in Parallel GC) slides all live objects in old generation together in one stop-the-world operation that can take seconds on large heaps. G1 compacts incrementally - it evacuates one region at a time during mixed GC. Each evacuation is short, and G1 spreads compaction work across multiple GC cycles. The trade-off is that G1 may take longer to fully compact, but pauses remain bounded and predictable.

8. What is the remembered set overhead in G1?

Each G1 region maintains a remembered set tracking references from other regions into that region. This adds memory overhead (roughly 10% of heap) but enables G1 to collect regions independently without scanning the entire heap. Without remembered sets, G1 would need to scan the whole heap for cross-region references, losing its incremental nature. The overhead is a worthwhile trade-off for predictable pause times.

9. What is the trade-off between MaxGCPauseMillis and throughput in G1?

MaxGCPauseMillis is a target, not a maximum-pause guarantee. A more aggressive target can change collection sizing and CPU use, but the result depends on the heap and workload. Compare pause distribution, throughput, and GC CPU under representative load instead of assuming that a looser target will reduce overhead.

10. What is floating garbage in CMS and G1?

Floating garbage is objects that become unreachable during concurrent marking but are not reclaimed until the next cycle. Since concurrent marking runs while the application runs, objects modified during marking may become unreachable but are not caught. CMS has the worst floating garbage because it does not compact and may accumulate garbage across cycles. G1 handles it better through its cleanup phases.

11. Why does G1 divide the heap into fixed-size regions?

Fixed-size regions (1MB to 32MB) enable G1 to collect arbitrary subsets of the heap without contiguity constraints. Old generation does not need to be one block - it is a collection of old regions. Young generation can grow and shrink dynamically by adding or removing regions. This flexibility lets G1 balance young/old ratio dynamically and do incremental compaction without moving the entire old generation at once.

12. What is the relationship between G1HeapRegionSize and heap size?

G1HeapRegionSize must be a power of 2 between 1MB and 32MB. The JVM selects region size based on heap size: smaller heaps get 1MB regions, larger heaps get 2MB, 4MB, 8MB, 16MB, or 32MB. With very large heaps (16GB+), use 16MB or 32MB regions to avoid having millions of regions which increases remembered set overhead and management cost. With small heaps, 1MB regions provide finer-grained collection sets.

13. What is evacuation failure in G1?

Evacuation failure (to-space exhausted or to-space overflow) occurs when G1 tries to copy survivor objects during young or mixed GC but runs out of free regions. The objects that cannot be copied remain in place (not collected), and G1 may have to fall back to a slower path. Fix by increasing -XX:G1ReservePercent (more reserve regions), increasing total heap, or reducing pause target to collect more frequently with less work per cycle.

14. How does InitiatingHeapOccupancyPercent work in G1?

IHOP controls when G1 starts the concurrent marking cycle. When old generation occupancy exceeds this percentage, G1 initiates a concurrent mark in the background. The default is 45%. Lower values start marking earlier (more CPU overhead, less old gen headroom needed). Higher values delay marking (less CPU overhead, risk that old gen fills before marking completes, triggering a full GC). Tune based on allocation rate - workloads that fill old gen quickly need lower IHOP.

15. Why does CMS require more heap than G1 under memory pressure?

CMS does not compact, so it needs contiguous free space to allocate into. As fragmentation increases, CMS needs more total free memory to find space for new allocations - even though individual fragments are small. G1 compacts incrementally and can reclaim fragmented regions. Under the same memory pressure, CMS will hit concurrent mode failure with less fragmentation than G1 requires for evacuation failure.

16. What is the young generation collection strategy in G1 and how does it differ from previous collectors?

G1 performs young GC ( Eden + Survivor regions -> new Survivor/old regions) as a stop-the-world operation using multiple threads. Unlike Parallel GC which treats young generation as fixed spaces, G1 dynamically adjusts the number of young regions based on the pause time target. G1 collects young regions by evacuating live objects to survivor regions or promoting them to old regions. The pause time target controls how many regions are included in each young GC, making pause times more predictable than Parallel GC's all-or-nothing approach.

17. How does G1's concurrent marking differ from CMS's implementation?

G1's concurrent marking uses a snapshot-at-the-beginning (SATB) algorithm that marks objects that were live at the start of marking, even if they become unreachable during marking. This allows marking to proceed without slowing down application threads. CMS uses incremental update (or original) that requires re-scanning modifiied objects, which can miss some newly floating garbage. G1's SATB is faster but produces more floating garbage. G1 also integrates marking with young and mixed GC cycles, while CMS runs marking as a separate phase before sweeping.

18. What is the mixed GC phase in G1 and when does it occur?

Mixed GC occurs after concurrent marking completes, when G1 collects a mix of young regions and old regions with low live ratio. During a mixed GC, G1 evacuates live objects from the collection set (which now includes old regions) to other regions, updating references in the process. The number of mixed GC cycles is controlled by parameters. After enough mixed GC cycles, old gen has been cleaned up sufficiently and G1 returns to young-only collections until the next concurrent marking cycle.

19. How does G1 handle large objects larger than half a region size?

Objects larger than half a G1 region are called humongous objects. They are allocated into contiguous humongous regions that are either one region (if they fit in half a region) or multiple regions. Humongous objects are collected during concurrent marking and cleanup, not during normal young or mixed GC. They cannot be moved during GC because moving would require updating references in global data structures. This makes them a fragmentation risk and a collection priority in G1's cleanup phases.

20. What is the G1 Heap Region Size formula and how should you configure it for different heap sizes?

G1 Heap Region Size must be a power of 2 between 1MB and 32MB. The JVM calculates it based on minimum region size of 1MB and maximum of 32MB, with the goal of having 2048 regions (minimum). For heaps under 4GB, 1MB or 2MB regions work well. For heaps between 4GB and 16GB, 4MB or 8MB regions. For heaps above 16GB, 16MB or 32MB regions reduce the remembered set overhead since each region has its own remembered set. Having too many small regions increases bookkeeping overhead.

Further Reading

Conclusion

CMS was deprecated in JDK 9 and removed in JDK 14. Use a collector supported by the exact JDK and vendor build, then benchmark it against the application latency and throughput requirements.

Category

Related Posts

ZGC and Shenandoah: Ultra-Low Latency Garbage Collectors

How ZGC and Shenandoah achieve sub-millisecond pause times through concurrent operations and load barriers, without stopping your application.

#jvm #garbage-collection #zgc

GC Fundamentals: Mark-Compact, Copying, and Mark-Sweep

A JVM-focused guide to mark-sweep, mark-compact, and copying algorithms, their heap trade-offs, and how real collectors schedule GC work.

#jvm #garbage-collection #gc-algorithms

JVM GC Tuning: Heap Sizing and Threshold Optimization

Practical strategies for sizing JVM heap, tuning generation ratios, and optimizing GC thresholds to reduce pause times and improve throughput.

#jvm #garbage-collection #heap-tuning