Garbage collector types

Garbage collector types

In this tutorial, we are going to discuss about Garbage collector types in java. Garbage collectors in Java are responsible for automatically managing memory by identifying and reclaiming memory occupied by objects that are no longer in use.

There are several types of garbage collectors available in Java, each designed to meet specific performance and responsiveness requirements.

Garbage collector types

Here are the commonly used garbage collector types in Java:

  1. Serial Garbage Collector
  2. Parallel Garbage Collector
  3. CMS Garbage Collector
  4. G1 Garbage Collector
  5. Z Garbage Collector (ZGC)
  6. Shenandoah Garbage Collector
1. Serial Garbage Collector

Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs. Turn on the -XX:+UseSerialGC JVM argument to use the serial garbage collector.

2. Parallel Garbage Collector

Parallel garbage collector is also called as throughput collector. It is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection. Turn on the -XX:+UseParallelGC JVM argument to use the parallel garbage collector.

3. CMS Garbage Collector

Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads in the following two scenarios only,

  1. While marking the referenced objects in the tenured generation space.
  2. If there is a change in heap memory in parallel while doing the garbage collection.

In comparison with parallel garbage collector, CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector. Turn on the XX:+USeParNewGC JVM argument to use the CMS garbage collector.

4. G1 Garbage Collector

G1 garbage collector is used for large heap memory areas. It separates the heap memory into regions and does collection within them in parallel. G1 also does compacts the free heap space on the go just after reclaiming the memory. But CMS garbage collector compacts the memory on stop the world (STW) situations. G1 collector prioritizes the region based on most garbage first. Turn on the –XX:+UseG1GC JVM argument to use the G1 garbage collector.

5. Z Garbage Collector (ZGC)

The Z Garbage Collector is an experimental garbage collector introduced in JDK 11 with a focus on low-latency and high-throughput garbage collection.ZGC is designed to handle very large heaps (multi-terabyte) with ultra-low pause times (typically less than 10 milliseconds).It uses advanced techniques such as colored pointers and concurrent compaction to minimize garbage collection pauses while maximizing application throughput.

6. Shenandoah Garbage Collector

Shenandoah Garbage Collector is another experimental garbage collector introduced in JDK 12 with similar goals as ZGC. Shenandoah Garbage Collector uses concurrent evacuation and compaction techniques to minimize garbage collection pauses. Designed for large heaps and applications with stringent latency requirements.

Overall, Shenandoah GC offers promising improvements in garbage collection pause times and performance for large-scale Java applications, particularly those with demanding latency requirements.

That’s all about the Garbage collector types in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Garbage collector types
Scroll to top