JVM Performance
About ZGC
Latest Updates in JDK 24
Generational ZGC Improvements
- Optimized Inter-Generational Reference Processing - The algorithms for handling references between generations have been refined, improving efficiency when objects in the old generation reference objects in the young generation.
- Enhanced Remembered Set Management - Improved handling of the remembered set (the data structure tracking old-to-young references) reduces overhead and improves scalability in large applications.
- More Adaptive Generation Sizing - The proportions of heap allocated to young and old generations are now more dynamically adjusted based on application behavior patterns.
Memory Management Enhancements
- Improved Memory Return Mechanism - The process of returning unused memory to the operating system has been optimized to be less intrusive to application performance, particularly valuable in container environments where resource efficiency is critical.
- Large Heap Optimization - For very large heaps (8TB+), memory mapping efficiency has been improved, enabling better performance in memory-intensive enterprise applications.
- Reduced Memory Fragmentation - Enhanced compaction algorithms minimize fragmentation more effectively, improving memory utilization in long-running applications.
Diagnostic Improvements
- Enhanced GC Logging - More detailed and structured logging helps identify potential issues and optimization opportunities. New log categories provide insight into specific ZGC operations.
- Extended JFR Events - Java Flight Recorder now includes additional ZGC-specific events, allowing for more granular performance analysis of garbage collection activities.
- Improved JMX Metrics - New management metrics have been added for monitoring ZGC's operation through JMX, aiding in real-time monitoring solutions.
Configuration and Tuning
-
Further Reduced Need for Manual Thread Sizing - The automatic thread management has been improved, making
-XX:ConcGCThreads
even less necessary in most deployments. - More Efficient Default Behavior - Out-of-the-box performance is improved, with better default settings for most common workloads.
Future Direction
- Further Parallel Processing Optimization - Enhancing the scalability of ZGC on many-core systems, allowing efficient utilization of increasingly parallel hardware.
- Refined Generational Strategies - More sophisticated object aging and promotion policies to better distinguish between short-lived and long-lived objects.
- Tighter Container Integration - Improved awareness and adaptation to containerized environments, particularly in cloud-native deployments.
JDK 24 Upgrade Recommendation
If you're using ZGC in production environments, upgrading to JDK 24 is recommended, especially for applications that benefit from generational garbage collection. The stability and performance improvements provide meaningful benefits with minimal migration effort.
Z Garbage Collector
-XX:+UnlockExperimentalVMOptions
and -XX:+UseZGC
options. For JDK 15 and later, only the -XX:+UseZGC
option is needed.- Concurrent
- Region-based
- Compacting
- NUMA-aware
- Using colored pointers
- Using load barriers
- Using store barriers (in the generational mode)
Configuration & Tuning
-Xmx
).-XX:+UseZGC
and -XX:+ZGenerational
options.-XX:+UseZGC
option.Setting Heap Size
Headroom
-Xmx
). This buffer allows the system to create more objects or process more data while helping reduce GC latency.the maximum heap size with the -Xmx
option. Since ZGC is a concurrent collector, you must choose a heap size that provides sufficient headroom for memory allocation while the service operates during GC activity and can accommodate the application's live-set. The headroom size depends on the application's allocation rate and live-set size. Generally, providing more heap memory to ZGC is beneficial, but excessive heap sizes are not recommended, so finding the right balance is important.Another heap size-related option in ZGC is -XX:SoftMaxHeapSize
, which sets a soft limit on how large the heap can grow. ZGC tries not to exceed this limit but may grow up to the maximum heap size if necessary. This exceeds the soft limit only when needed to prevent the application from pausing while waiting for GC to reclaim memory. For example, with -Xmx5g -XX:SoftMaxHeapSize=4g
, ZGC typically uses up to 4GB but can temporarily use up to 5GB.
Setting Concurrent GC Threads
-XX:ConcGCThreads=<number>
. ZGC has heuristics to automatically set the thread count, which generally works well but may need adjustment based on application characteristics. This option fundamentally determines how much CPU time to allocate to GC. Setting too many threads can cause GC to take excessive CPU time from the application, leading to performance degradation. Setting too few can cause the application to allocate garbage faster than GC can collect it.Returning Unused Memory to the Operating System
-XX:-ZUncommit
option. Additionally, memory is not uncommitted below the minimum heap size (-Xms
). Setting -Xmx
and -Xms
to the same value implicitly disables this default behavior.-XX:ZUncommitDelay=<seconds>
(default 300 seconds). This specifies how long memory must remain unused before becoming eligible for uncommitting. By default, memory unused for 300 seconds (5 minutes) is uncommitted and returned to the OS.-Xmx
and -Xms
to the same value. Also, use the -XX:+AlwaysPreTouch
option (supported from JDK 14) to move memory to pages. However, this forces all allocated heap memory pages to be pre-mapped to physical memory when the JVM starts, which can cause problems in container-based cloud environments if it uses more memory than allocated to the container.Using Large Pages
Enabling NUMA (Non-Uniform Memory Access) Support
-XX:+UseNUMA
or -XX:-UseNUMA
. When running on NUMA machines (e.g., a multi-socket x86 machine), having this option enabled can provide noticeable performance improvements.Conclusion
- Need consistent performance with minimal pauses
- Work with large heaps (multi-GB to TB scale)
- Have strict response time requirements
- Run on modern multi-core systems
References
Advertisement