Skip to main content

7.2 Types of Garbage Collectors

← Back to Chapter 7: Garbage Collection & Serialization

Selecting the right GC collector depends on application priorities: throughput, pause latency, or memory footprint.


1. Evolution of HotSpot GC Collectors​

CollectorJVM FlagTarget WorkloadSTW Pause Duration
Serial GC-XX:+UseSerialGCSingle-core micro-containersHigh (Single-threaded STW)
Parallel GC-XX:+UseParallelGCBatch processing, high throughputMedium-High (Multi-threaded STW)
G1 GC (Default)-XX:+UseG1GCGeneral enterprise web appsLow (Configurable, e.g. 200ms)
ZGC-XX:+UseZGCUltra-low latency enterprise APIsSub-millisecond (< 1 ms)
Shenandoah-XX:+UseShenandoahGCUltra-low latency concurrentSub-millisecond (< 1 ms)

Serial Garbage Collector​

Uses essentially one GC thread for collection work.

Good for:

small applications
small heaps
environments where simplicity matters

Not ideal for large, latency-sensitive applications.

Parallel Garbage Collector​

Parallel Garbage Collector

Uses multiple threads for GC work.

GC
├── Thread 1
├── Thread 2
├── Thread 3
└── Thread 4

Goal: High throughput

Throughput means:

Spend more time doing application work and less time doing GC work.

G1 Garbage Collector​

G1 = Garbage-First Garbage Collector

Instead of thinking only in terms of one large contiguous Young/Old area, G1 divides the heap into many regions.

Heap :

┌──┬──┬──┬──┐
│ │ │ │ │
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│ │ │ │ │
└──┴──┴──┴──┘

Some of them are young object and some of them are old object. G1 tries to prioritize regions with lots of garbage.

Why is G1 useful?​

G1 was designed for applications where you want:

relatively predictable pauses
large heaps
good overall performance
better control of pause targets

ZGC​

ZGC is designed for very low pause times, including large heaps.

Its key idea is:

Perform much of the GC work concurrently with application threads.

So:

Application ────────────────>
↑
│
GC
│
└──── mostly concurrent

It is useful for applications where latency is extremely important.

Shenandoah GC​

Shenandoah also focuses on: Very low GC pause times

It performs significant work concurrently with application execution.

Conceptually:

Application ────────────────>
GC ────────────────>

They overlap heavily.

Ways of Garbage Collection :​

finalize() (Deprecated)​

finalize() was mainly used for cleanup of external resources such as files, connections, etc.

Try-with-resources — preferred​

try (FileInputStream file = new FileInputStream("data.txt")) {
// Use the file
} // File is automatically closed here

This is resource cleanup, not GC.

AutoCloseable​

Your class can implement AutoCloseable:

class MyResource implements AutoCloseable {

@Override
public void close() {
// Release the resource
}
}

try (MyResource resource = new MyResource()) {
// Use resource
}

Cleaner​

Java also provides java.lang.ref.Cleaner.

It can be used for special cases where some cleanup action is needed as a safety net, but it should not be treated as a replacement for normal explicit resource management.