Which garbage collection algorithms should a Java developer know, and how do they differ?
Five are worth knowing, and they differ mainly in how they trade throughput against pause times.
Serial uses a single thread and a single compacting collector. It has the lowest overhead of all of them and the shortest throughput, which makes it the right choice for small heaps and for command-line tools where start-up time matters more than throughput.
Parallel — also called the throughput collector — uses multiple threads for both the young and the old collections. It gives the best throughput of the lot at the cost of longer pauses, and it is the traditional default for batch processing and for large-heap server workloads where you care about total work done rather than tail latency.
G1 divides the heap into regions of equal size rather than into contiguous spaces, and collects the regions with the most garbage first. It aims for a pause-time target you set, and it compacts as it goes. That makes it a good general-purpose default, and it is the collector to reach for when you need predictable pauses on a large heap without going to the very latest collectors.
ZGC and Shenandoah are the low-latency collectors. Both do most of their work concurrently, using coloured pointers and load barriers so that the garbage collector can move objects around without stopping the application threads. ZGC aims for sub-millisecond pauses, and Shenandoah does something similar with a different implementation strategy. They are the right choice when you have a strict latency service and a large heap.
The selection rule of thumb: default to G1 unless you have a specific reason, choose Parallel for pure throughput, and choose a low-latency collector when tail latency is the thing you are measured on.