☕ Java Q57 / 162

What are different GC algorithms?

AI-Powered Answer ✓ Answered

Garbage Collection (GC) in Java is an automatic memory management process that identifies and reclaims memory occupied by objects that are no longer referenced by the program. Different GC algorithms are designed to optimize for specific goals, such as throughput, pause times, or memory footprint, making them suitable for various application types and workloads.

Serial GC

The Serial Garbage Collector is the simplest GC algorithm. It uses a single thread to perform all garbage collection work and thus, it 'stops the world' (pauses all application threads) during collection cycles. It is suitable for single-processor machines or applications with small heap sizes where long pauses are acceptable.

Parallel GC (Throughput Collector)

The Parallel Garbage Collector, also known as the Throughput Collector, was the default GC in Java 8. It's similar to the Serial GC in that it also performs stop-the-world collections, but it uses multiple threads to perform garbage collection, primarily in the young generation, significantly speeding up the collection process. Its primary goal is to maximize overall application throughput, making it suitable for applications that can tolerate occasional longer pauses.

CMS GC (Concurrent Mark-Sweep)

The Concurrent Mark-Sweep (CMS) Garbage Collector was designed to minimize application pause times by performing most of its work concurrently with the application threads. It aims for low-latency operation by avoiding full stop-the-world collections. However, it can consume more CPU resources, may lead to heap fragmentation, and can still have short stop-the-world phases. It has been deprecated and removed in recent Java versions (JDK 14 onwards).

G1 GC (Garbage-First)

The G1 (Garbage-First) Garbage Collector is a server-style GC introduced in Java 7 and became the default in Java 9. It divides the heap into a set of equal-sized regions and aims to achieve configurable soft real-time goals (i.e., 'pause time targets') while maintaining good throughput. G1 prioritizes collecting regions that contain the most garbage first, hence 'Garbage-First.' It works well for applications with large heaps (many GBs) that require predictable pause times.

ZGC

ZGC is a scalable low-latency garbage collector designed for applications that require very low pause times (less than 10 ms) and/or use very large heaps (multi-terabyte). It performs almost all GC work concurrently with the application threads, minimizing stop-the-world pauses. ZGC became generally available in Java 15 and is suitable for high-performance computing and microservices.

Shenandoah GC

Shenandoah is another low-pause-time garbage collector that aims to reduce GC pause times to milliseconds, regardless of heap size. Like ZGC, it performs most of its work concurrently with the application, allowing application threads to continue execution during garbage collection cycles. Shenandoah is an OpenJDK project that was integrated into OpenJDK 12 and offers very similar benefits to ZGC in terms of low-latency operation for large heaps.