Garbage collectors are concrete implementations of garbage collection algorithms like Mark-Sweep, Mark-Compact, and Copying. The default garbage collector can vary between different Java Development Kit (JDK) versions.
There are seven common garbage collectors: Serial, Serial Old, ParNew, Parallel Scavenge, Parallel Old, CMS, and G1.
To identify the default garbage collector for your JDK version, you can add the VM option -XX:+PrintCommandLineFlags to your Java program's startup configuration. For example, on Java 8, the default is ParallelGC.
Garbage collectors can be categorized by whether they target the young or old generation of the heap. Some collectors can be used in combination. For instance, Serial GC can be paired with CMS GC or Serial Old GC. The G1 garbage collector, however, is capable of working on both the young and old generations.
Stop-the-World (STW) Mechanism
STW refers to a global pause where all application threads are halted during garbage collection until the process is complete. While Java code execution stops, native code can still run, but it cannot interact with the JVM.
Serial Collector
The Serial collector targets the young generation, uses the copying algorithm, and operates in a serial manner. It's the default young generation GC for client-level JVMs and utilizes the STW mechanism. You can explicitly enable it with -XX:+UseSerialGC, which also implies the use of Serial Old for the old generation.
Serial Old Collector
Designed for the old generation, Serial Old employs the Mark-Compact algorithm. It's the old generation counterpart to the Serial collector, also using serial collection and STW. It serves as the default old generation GC for client-level JVMs and acts as a fallback for the Parallel Scavenge and CMS collectors in server mode.
ParNew Collector
This collector is for the young generation and uses the copying algorithm. It's a multi-threaded version of the Serial collector, performing parallel collection. This results in better performance than Serial on multi-core CPUs, though it offers no advantage on single-core systems. Like Serial, it uses the STW mechanism. It can be specified with -XX:+UseParNewGC, and the thread count can be set using -XX:ParallelGCThreads=X.
Parallel Scavenge Collector
A parallel collector for the young generation, Parallel Scavenge uses the copying algorithm and prioritizes high throughput. It aims to maximize CPU utilization and employs the STW mechanism. With a throughput of around 99% (defined as user thread time divided by total thread time), it's suitable for applications where responsiveness is not critical, such as background tasks. It's the default GC for server-level JVMs.
Parallel Old Collector
Parallel Old is the old generation counterpart to Parallel Scavenge. It uses the Mark-Compact algorithm and operates in parallel with STW, also prioritizing throughput. It can be enabled with -XX:+UseParallelOldGC. The thread count is configured via -XX:ParallelGCThreads=X. A common heuristic is to set threads equal to CPU cores for fewer than 8 cores, and 3 + 5 * num_cores / 8 otherwise. This is the default GC in Java 8.
CMS (Concurrent Mark Sweep) Collector
CMS targets the old generation and uses the Mark-Sweep algorithm, aiming for low pause times and high concurrency. It interleaves garbage collection threads with application threads, though it still involves STW phases. This results in higher CPU usage but quicker response times and shorter pauses, making it a choice for multi-core systems prioritizing responsiveness. CMS was deprecated in JDK 9 and removed in JDK 14.
Its operation involves:
- Initial Mark (STW): A brief pause to mark objects directly reachable from GC Roots.
- Concurrent Mark: Traverses the object graph from GC Roots. This phase runs concurrently with application threads and has no STW.
- Remark (STW): A second pause to address any changes in object marking that occurred during the concurrent mark phase.
- Concurrent Sweep: Cleans up objects marked for deletion.
- Concurrent Reset: Resets internal data structures for the next cycle.
It can be specified using -XX:+UseConcMarkSweepGC.
CMS Drawbacks:
- Floating Garbage: New objects created during the concurrent sweep phase are not collected until the next cycle.
- Memory Fragmentation: Due to the Mark-Sweep algorithm, fragmentation can occur. Compacting memory during concurrent phases is not feasible.
- Increased CPU Usage: The concurrent nature requires more CPU resources.
- Larger Heap Requirement: CMS needs extra heap space to accommodate new object allocations during its cycle. If this space runs out, a
Concurrent Mode Failurecan occur, triggering a fallback to Serial Old. CMS typically initiates collection when the old generation reaches 68% occupancy, configurable with-XX:CMSInitiatingOccupancyFraction=n.
G1 (Garbage-First) Collector
G1 became the default GC in JDK 9. Its goal is to achieve high throughput while maintaining predictable pause times. It divides the heap into regions and prioritizes collecting regions with the most garbage. This approach is also known as 'Garbage First'.
Key characteristics include:
- Regionalization: Divides the heap into regions, logically distinguishing young and old generations.
- Humongous Regions: A special type of region for large objects (over 50% of a region's capacity). If a Humongous region cannot accommodate an object, G1 seeks contiguous Humongous regions, potentially triggering a Full GC if none are found.
- Mark-Compact and Copying: Uses Mark-Compact overall but employs copying within regions.
- Parallel and Concurrent: Combines parallel and concurrent collection.
- Space Compaction: Avoids memory fragmentation by compacting regions.
- Predictable Pause Times: Allows users to specify a target pause time.
ZGC Garbage Collector
ZGC aims to limit garbage collection pause times to under 10ms, regardless of heap size, with minimal impact on throughput. Its phases include concurrent marking, concurrent pre-unmapping, concurrent unmapping, and concurrent remapping.
ZGC was restricted to Linux environments before JDK 14. Other vendors, such as Alibaba, have developed their own collectors like AliGC, based on G1 and optimized for large heaps.