JEP draft: G1: Time-Based Heap Uncommit During Idle Periods

OwnerMonica Beckwith
TypeFeature
ScopeImplementation
StatusClosed / Withdrawn
Componenthotspot / gc
Discussionhotspot dash gc dash dev at openjdk dot java dot net
EffortM
DurationM
Created2025/06/28 04:30
Updated2025/07/03 21:02
Issue8360935

Summary

Introduce time-based heap sizing for the G1 garbage collector to automatically uncommit unused memory regions during application idle periods, independent of garbage collection frequency.

Goals

Non-Goals

Success Metrics

G1 should automatically release unused Java heap memory during application idle periods without requiring garbage collection activity. Success is measured by:

Motivation

Current G1 heap sizing depends on garbage collection activity. Applications with infrequent garbage collections may retain committed memory unnecessarily during idle periods, leading to poor memory efficiency in containerized environments and multi-tenant systems. This enhancement addresses the limitation described in JDK-8357445 by providing regular heap size re-evaluation independent of GC cycles.

Collector Parity: ZGC already provides memory uncommitting during idle periods without requiring garbage collection triggers. This enhancement brings G1 to parity with ZGC's memory efficiency characteristics while maintaining G1's low-latency and predictable pause time properties, using similar uncommit delay defaults (5 minutes) but with more predictable evaluation scheduling.

Modern cloud-native applications often have variable workload patterns with significant idle periods, during which committed heap memory cannot be reclaimed by the operating system without garbage collection activity. This creates unnecessary memory pressure in containerized deployments and reduces overall system efficiency.

Description

This enhancement introduces a periodic background task that evaluates heap regions for uncommit based on inactivity time rather than GC-driven metrics.

Key Components

Region Activity Tracking: Each heap region maintains a timestamp that is updated when the region is cleared for reuse and when allocation regions are retired.

Evaluation Task: A configurable periodic task (G1HeapEvaluationTask) evaluates regions for uncommit eligibility.

Uncommit Policy: Regions are considered for uncommit if they are empty and have been inactive longer than a configurable delay period.

Configuration

Four new command-line flags control the behavior:

-XX:+G1UseTimeBasedHeapSizing (default: false, EXPERIMENTAL)
  Enables time-based heap sizing. Requires -XX:+UnlockExperimentalVMOptions.

-XX:G1TimeBasedEvaluationIntervalMillis=<value> (default: 60000, MANAGEABLE, range: 1000-3600000)
  Interval in milliseconds between heap evaluations. 

-XX:G1UncommitDelayMillis=<value> (default: 300000, MANAGEABLE, range: 1000-7200000)
  Minimum time in milliseconds a region must be inactive before uncommit eligibility.

-XX:G1MinRegionsToUncommit=<value> (default: 10, EXPERIMENTAL, range: 1-1000)
  Minimum number of eligible regions required before uncommit occurs.

Implementation

The evaluation task operates independently of GC cycles using existing G1 service task infrastructure. Region activity is tracked with minimal overhead at existing lifecycle transition points.

Log Output

When enabled, the feature produces logging output at initialization and during evaluation:

Initialization:

[2025-06-27T19:16:13.618+0000][info][gc,init] G1 Time-Based Heap Sizing enabled (uncommit-only): evaluation_interval=30000ms, uncommit_delay=60000ms, min_regions_to_uncommit=2

Evaluation Activity:

[2025-06-27T19:16:13.656+0000][debug][gc,sizing] Starting heap evaluation
[2025-06-27T19:16:13.656+0000][debug][gc,sizing] Full region scan: found 0 inactive regions out of 1736 total regions

[2025-06-27T19:17:43.659+0000][debug][gc,sizing] Uncommit candidates found: 63 inactive regions out of 1736 total regions
[2025-06-27T19:17:43.659+0000][info ][gc,sizing] Time-based uncommit: found 63 inactive regions, uncommitting 10 regions (80MB)
[2025-06-27T19:17:43.659+0000][info ][gc,sizing] Time-based evaluation: shrinking heap by 80MB
[2025-06-27T19:17:43.662+0000][info ][gc,heap] Heap shrink completed: uncommitted 10 regions (80MB), heap size now 776MB

[2025-06-27T19:19:13.669+0000][info ][gc,sizing] Time-based uncommit: found 271 inactive regions, uncommitting 61 regions (488MB)
[2025-06-27T19:19:13.669+0000][info ][gc,heap] Heap shrink completed: uncommitted 61 regions (488MB), heap size now 4408MB

[2025-06-27T19:22:13.686+0000][debug][gc,sizing] Starting heap evaluation
[2025-06-27T19:22:13.686+0000][debug][gc,sizing] Full region scan: found 0 inactive regions out of 1736 total regions
[2025-06-27T19:22:13.687+0000][info ][gc,sizing] Time-based evaluation: no heap uncommit needed (evaluation #10)

Alternatives

  1. GC-Triggered Evaluation
  1. Allocation-Pressure Based
  1. External Memory Pressure API
  1. Statistics-Based Thresholds
  1. Immediate Uncommit

Testing

JTReg Test Suite

Core Functionality Tests:

Stress Testing:

Performance Benchmarking

SPECjbb2015 Comprehensive Testing:

Platform Validation

Multi-Platform Testing:

Risks and Assumptions

Performance Risks

Functional Risks

Assumptions

Dependencies

Acknowledgements

This enhancement builds upon the existing G1 garbage collector infrastructure and benefits from the robust foundation provided by years of G1 development and optimization. The time-based approach complements existing sizing mechanisms while maintaining G1's performance characteristics.