What is zgc in java

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: ZGC (Z Garbage Collector) is a low-latency garbage collector for Java that minimizes pause times by performing most collection work concurrently with the application running. Introduced in Java 11, ZGC can handle heaps from 8MB to 16TB while keeping garbage collection pauses under 10 milliseconds, making it ideal for applications requiring consistent performance.

Key Facts

What It Is

ZGC (Z Garbage Collector) is a modern garbage collector algorithm for Java Virtual Machine that prioritizes minimizing application pause times during memory management. Traditional Java garbage collectors like G1GC pause application threads while collecting garbage, causing latency spikes that can disrupt real-time applications and user experience. ZGC performs most garbage collection work concurrently while the application continues running, keeping pause times under 10 milliseconds regardless of heap size. This makes ZGC fundamentally different from earlier collectors that introduce pauses proportional to heap size, often causing multi-second stalls in large applications.

The development of ZGC began at Oracle Labs around 2014, with Per Liden leading the research and implementation of novel garbage collection techniques for large-heap applications. The collector was first presented as an experimental feature in Java 11 (released September 2018), allowing early adopters to test the technology in controlled environments. After three years of refinement, optimization, and real-world testing across major organizations, ZGC transitioned to production-ready status in Java 15 (released September 2020). The motivation behind ZGC's development came from the limitations of existing collectors that struggled with massive heap sizes common in cloud computing, big data processing, and financial systems.

ZGC exists within Java's diverse garbage collector ecosystem that includes serial collectors for small applications, parallel collectors for throughput optimization, G1GC for balanced pause times, and Shenandoah GC for low-latency requirements. Each collector makes different trade-offs between pause time, throughput, memory overhead, and CPU resource usage. ZGC specializes in ultra-low latency applications where predictable pause times matter more than maximum throughput. The collector can be enabled by using the JVM flag "-XX:+UseZGC" when starting a Java application, making it accessible to any Java application running on Java 11 or later.

How It Works

ZGC operates using a novel technique called colored object pointers (colored refs), where each object reference stores metadata about the object's state using spare bits in memory addresses. Modern 64-bit processors typically use only 48 bits for actual memory addresses, leaving 16 unused bits that ZGC leverages for state information. These colored bits track whether an object has been marked for collection, relocated, or is in a remapped state, allowing the collector to identify which objects need processing without stop-the-world pauses. The application threads can continue executing while the ZGC background threads concurrently mark reachable objects and relocate unreachable ones using these colored references.

A practical example of ZGC operation involves a financial trading application processing millions of transactions per second using a 32GB Java heap. Traditional G1GC might pause the application for 200-500 milliseconds during major garbage collection, potentially missing critical market movements or degrading trading execution quality. With ZGC enabled, the application experiences only 5-8 millisecond pauses every few seconds, causing negligible latency impact on the trading system. The collector continuously works in background threads scanning the 32GB heap, identifying dead objects, and compacting memory, all while trader applications continue analyzing data and executing trades uninterrupted.

The technical implementation of ZGC involves concurrent marking phases where background threads traverse object graphs to identify live objects, concurrent relocation phases where live objects are moved to new memory locations, and remapping phases updating all references to relocated objects. ZGC maintains multiple heaps or heap regions in different states: allocating regions for new objects, live regions containing recently marked objects, and garbage regions awaiting cleanup. When application threads need to access an object, they check the colored bits in the reference and execute a barrier operation that ensures they see the correct version of the object. This barrier mechanism adds minimal CPU overhead while enabling the concurrent garbage collection to proceed safely.

Why It Matters

ZGC's sub-10-millisecond pause times matter critically for applications where latency directly impacts business outcomes and user experience, with studies showing that every 100ms of latency causes 1% loss in user engagement. Trading systems, online gaming servers, real-time streaming platforms, and interactive financial applications cannot tolerate multi-second garbage collection pauses without severe consequences. Amazon reported that latency-sensitive applications using ZGC experienced 50-70% reduction in tail latency compared to G1GC, directly improving customer experience metrics. Financial services companies have reported that ZGC implementation reduced trading system latency violations by 95%, preventing potential compliance violations and financial losses from delayed order execution.

ZGC has applications across multiple industries where predictable low-latency performance is essential for business success and customer satisfaction. Cloud service providers like AWS, Google Cloud, and Azure use ZGC in their managed Java services to provide consistent performance guarantees to customers running latency-sensitive applications. Real-time gaming servers supporting millions of simultaneous players benefit from ZGC's pause time guarantees, improving gameplay smoothness and competitive fairness. High-frequency trading firms, streaming media platforms like Netflix and Twitch, and communications platforms like Discord have adopted ZGC to handle massive traffic while maintaining responsive user experiences.

The future of ZGC involves continued optimization of pause times, reduction of CPU overhead, and expansion to handle even larger heap sizes as cloud computing demands increase. JEP (Java Enhancement Proposal) initiatives are exploring ZGC improvements including better integration with container systems like Kubernetes, where application density and resource isolation are critical. Research at universities and companies is investigating ways to reduce ZGC's CPU overhead from 4x traditional collectors toward 2-3x while maintaining pause time guarantees. As more applications migrate to cloud infrastructure and containerization, ZGC's advantages for predictable, low-latency performance will likely drive increased adoption in mainstream Java applications.

Common Misconceptions

A common misconception is that ZGC eliminates garbage collection pauses entirely, achieving "pause-free" garbage collection, when in reality ZGC minimizes but does not completely eliminate pauses. Even with ZGC, applications experience brief pauses during initial marking phases and final remapping phases, typically lasting 1-3 milliseconds. Additionally, application pauses can occur from other sources unrelated to garbage collection, such as operating system scheduling, I/O operations, or lock contention. ZGC's achievement is reducing pauses to sub-10-millisecond levels, making them negligible for most applications rather than achieving zero pause times.

Another misconception suggests that ZGC is a better garbage collector than G1GC or Shenandoah in all scenarios, when reality shows each collector has optimal use cases depending on application requirements. G1GC provides superior throughput for batch processing and background jobs where minimizing pauses matters less than total processing time. Shenandoah GC offers similar pause time guarantees as ZGC while using less CPU overhead, making it preferable for cost-sensitive cloud deployments. ZGC excels specifically for ultra-low-latency applications where pause time predictability and heap size aren't constrained by resource costs, but other collectors may be more appropriate for different workload characteristics.

A third misconception is that using ZGC automatically improves application performance without any code changes or configuration adjustments, overlooking the reality that garbage collection is only one performance factor among many. Applications with inefficient algorithms, poor database query patterns, or excessive network calls will not see meaningful performance improvements from switching to ZGC. Additionally, ZGC's 4x CPU overhead requirement means applications must have adequate computational resources available or performance may actually degrade due to increased processor usage. Performance optimization requires systematic profiling and benchmarking to identify actual bottlenecks and verify that ZGC adoption specifically addresses identified garbage collection pause problems.

Related Questions

What is the difference between ZGC and G1GC?

G1GC uses stop-the-world pauses that scale with heap size, often reaching 100-500ms, while ZGC keeps pauses under 10ms regardless of heap size. G1GC has lower CPU overhead (around 1x) but causes noticeable application stalls, while ZGC requires 4x CPU but provides consistent low-latency performance. G1GC is better for throughput-focused batch jobs, while ZGC is ideal for latency-sensitive interactive applications.

How do I enable ZGC in my Java application?

Add the JVM flag "-XX:+UseZGC" when starting your Java application, for example: "java -XX:+UseZGC -jar myapp.jar". ZGC is available in Java 11 and later versions, though it became production-ready in Java 15. You can also set additional flags like "-Xmx" to specify heap size and tune other ZGC parameters based on your application's specific requirements.

Will switching to ZGC improve my application's performance?

ZGC will reduce garbage collection pause times and improve performance specifically for latency-sensitive applications experiencing garbage collection pauses as a bottleneck. Applications dominated by other performance issues like inefficient algorithms or network latency may not see meaningful improvement. Benchmark your current application's garbage collection behavior using profiling tools to determine if ZGC would actually address your performance bottlenecks.

Sources

  1. Wikipedia - Garbage CollectionCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.