Week 71 — Which garbage collectors does Java provide and what are the differences between them?
Question of the Week #71
Which garbage collectors does Java provide and what are the differences between them?
3 Replies
As different applications have different needs, there are multiple garbage collectors available in Java.
One such GC which optimizes for minimal overhead and maximum throughput is ParallelGC. This collector stops the entire application while doing GC work which can introduce significant pause times in which the application is unresponsive. ParallelGC is useful for batch applications that don't need to respond quickly. It can be enabled using the JVM argument
-XX:+UseParallelGC
.
Another garbage collector is G1 which is capable of doing most of the work while the application is running but it still needs to pause the application to do some of its work. G1 is selected as the default GC in most Java applications. G1 can be enabled using -XX:+UseG1GC
.
In JDK 11, a new GC named ZGC has been added. ZGC tries to optimize pause times and should capable of achieving pause times that are less than 1ms even for heaps up to 16TB in newer Java versions. It is primarily made for server applications that require low latency. ZGC performs all of its work in the background and only pauses the application for synchronization. It can be enabled with -XX:+UseZGC
Similar to ZGC, another garbage collector named Shenandoah has been added in JDK 12. As with ZGC, Shenandoah focuses on low pause times that do not increase with bigger heaps. Shenandoah can be enabled with the option -XX:+UseShenandoahGC
.
Both Shenandoah and ZGC were marked production-ready in JDK 15.📖 Sample answer from dan1st
sgc (serial garbage collector): it uses only one thread for collecting, slow
paralell garbage collector: multiple threads and is fast
idk more
Submission from ashishagarwal2023