Since JDK 8, Java's garbage collection (GC) has undergone significant evolution, addressing common challenges like latency, pause times, and memory overhead. This article explores these advancements, focusing on practical implications for developers transitioning from older versions like JDK 8 to modern alternatives such as JDK 17 and JDK 21. Whether you’re maintaining legacy applications or planning future migrations, understanding these updates is crucial.
Garbage Collection (GC) in Java automates memory management, freeing developers from handling low-level details. The two primary goals of GC are:
This division is based on the generational hypothesis, which posits that most objects die young, making young generation collections more efficient than full heap collections. Java provides several GC algorithms, each tailored to specific use cases:
Garbage Collector | Focus | Use Case | Pause Time | Throughput | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Low memory overhead | Small containers | Medium | Low | ||||||||||||||||||||||||||||||
Parallel GC | High throughput | Batch processing or large datasets | High | High | ||||||||||||||||||||||||||||||
G1 GC | Balanced performance | General-purpose, low-latency workloads | Medium-Low | Medium-High | ||||||||||||||||||||||||||||||
ZGC | Ultra-low latency | Large-scale applications, low latency | Sub-millisecond | Medium | ||||||||||||||||||||||||||||||
Shenandoah GC | Low latency | Large heaps, near-real-time processing | Very low | Medium |
Introduced as the default collector in JDK 9, G1 GC uses a region-based heap layout and supports concurrent marking. This allows it to determine liveness without halting application threads. By combining young and old generation collections into smaller mixed collections, G1 reduces pause times and improves overall responsiveness.
Designed for ultra-low latency, ZGC can handle terabyte-sized heaps with pause times in the sub-millisecond range. It performs most of its work concurrently with application threads, making it ideal for applications requiring consistent responsiveness, such as cloud services or financial systems.
ZGC Generational Mode (introduced in JDK 21) further improves throughput by applying the generational hypothesis to separate short-lived and long-lived objects.
Benchmarks such as SPECjbb 2015 demonstrate substantial improvements in both throughput and latency across modern GC algorithms since JDK 8:
Pause times have been drastically reduced across all collectors:
G1 GC has seen significant reductions in native memory overhead, thanks to optimizations in remembered sets, data structures used for region-based collections. From JDK 8 to JDK 17, G1's native memory usage was cut almost in half. To better illustrate the practical aspects of GC, consider the following examples:
# Add these options to your JVM startup command java -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -Xmx2g -Xms2g -jar app.jar
This configuration:
java -XX:+UseZGC -Xms4g -Xmx4g -XX:SoftRefLRUPolicyMSPerMB=50 -jar app.jar
This setup:
While upgrading from JDK 8 to a newer version (e.g., JDK 17 or 21) can bring significant benefits, developers must consider:
The progress in Java's garbage collection since JDK 8 has been remarkable. With significant improvements in throughput, latency, and memory overhead, upgrading to newer JDK versions is necessary for any Java application.
Whether you're running small containers or large-scale cloud services, there's a GC algorithm optimized for your use case. So, if you're still on JDK 8, it's time to make leap and enjoy the performance benefits of modern Java.
For more information, watch this video from Devoxx Belgium about Garbage Collection in Java: The Progress Since JDK 8 by Stefan Johansson
?
The above is the detailed content of Garbage Collection in Java: Progress Since JDK 8. For more information, please follow other related articles on the PHP Chinese website!