A Brief Overview of Garbage Collectors in Java, Because Cleanliness Is Necessary.
By: Prateek Nima
Tags: Java, Interview, Software Development, Better Programming, Programming

The following are the 7 types of garbage collectors in Java along with description of each and their appropriate use cases. I will try to explain each of them briefly so that you can differentiate between each one of them without going through a long article.

1. Serial Garbage Collector

The serial GC is useful when you only want a single thread to perform all the GC operations. Whenever the garbage collector runs it freezes all the operations running in the background. Due to this, it won’t be a decent choice to use the serial garbage collector in a live server environment where the difference of every millisecond counts to judge the application performance. The other scenario where it would be useful to use serial GC is when there are a large amount of JVMs which run on the same machine and the GC threads would not interfere with each other.

Highlights:- Single thread, Freezes application operations, Not suitable for live environments, feasible when multiple JVMs on a single machine.

2. Parallel Garbage Collector(Also known as Throughput collector)

This was the default GC before Java 9. As the name signifies, the parallel GC uses multiple threads to clear up the memory in the heap space. The parallel GC likewise pauses all the processes in the background while performing its operations. One way to improve the throughput of application is to increase the heap space but on the other hand, this can lead to long pauses when garbage collection takes place.

As we now know that there are multiple threads which perform the garbage collection we control the number of threads which participate in garbage collection. By default, for N CPUs, N Threads are used to perform garbage collection. This collector should be usually used when a lot of work needs to be done and long pauses are admissible.

Highlights:- Multiple GC threads, Pauses background processes, large heap size might lead to longer pauses. no concurrent execution.

3. Concurrent Mark Sweep(CMS) Garbage Collector

As we have seen the parallel GC might lead to long pauses in the application. The Concurrent Mark Sweep(CMS) GC helps us to reduce this waiting time. As we are aware everything comes for a trade-off, we need to share the processor resources with the garbage collector in between the execution of the application. If we compare between parallel and CMS garbage collector, CMS should be preferred only when it is feasible for your application to share the resources with the garbage collector which would also eventually reduce the wait time.

Highlights:- Multiple threads, Less time compared to parallel GC, Processor resources shared, no concurrent execution.

4. Garbage-First Garbage Collector(G1 Garbage Collector)

G1 Garbage collector was first released in JDK version 7 and has been the default java collector since Java 9. The GC , in this case, consists of two phases

a) Marking Phase:- In this phase, the GC divides the heap space into an equal number of contiguous partitions.

b) Sweeping Phase:- After the Marking phase, the GC now knows which region has the least amount of space occupied and clears it as yield the most amount of free space.

Although using G1 collector reduces the wait time but it can lead to degrading performance as the heap size increases since if the heap size is very large it will waste all its time in phase 1 i.e. the marking phase itself. To overcome this issue Shenandoah GC was introduced which we will talk in the coming section.

Highlights:- Two phases, Marks and then clears the space, not feasible for large heap sizes, no concurrent execution.

5. Epsilon Garbage Collector

Introduced in Java 11, the Epsilon Garbage Collector was introduced by considering the scenario when you want to clear a certain amount of memory for your application i.e. it won’t perform any garbage collection. So your next question would be then what’s the point in using it. Whenever the JVM runs the garbage collection process it stops the other processes of your application while its performing GC but sometimes you want to test your application performance without being affected by any other factors. Epsilon is just simply impeccable to use in such cases as it removes the impact of GC on the application performance. This helps you to test your application and helps you to check the throughput without any interruption by the garbage collector.

While using the epsilon garbage collector you can specify how much heap memory you want to allocate for your application and then run it. The downside of the garbage collector is that it does not recycle the objects, not in use. So if the heap space is exhausted the application will run out of memory and throw a java.lang.OutOfMemoryError Exception.

Highlights:- Passive GC, Test application performance, application might run out of memory.

6. Z Garbage Collector(ZGC)

Introduced in Java 10, it helped in concurrent execution of the application without hampering the throughput of the application. Until Java 10 the G1 GC was the default collector in java. But the major issue with the earlier GCs was that they stop the execution of the current application program while the garbage collection is being carried out. This not only adds overhead to your application but also reduces the throughput and add up to the latency of your application.

Being a scalable low latency GC, it performs the expensive tasks concurrently within a time target of 10ms i.e. it does not stop the execution of your current application for more than 10ms. The most important factor in ZGC is setting up the heap size. The max heap size must be set such that it can accommodate and run your application, although it is better the more memory you provide there must be a ratio between the memory usage and how often the GC should run since wasting the memory is not acceptable.

Highlights:- Concurrent execution, specify max heap size, execution time of 10ms.

Image via blogs.oracle.com. Pause Time Comparison between different garbage collectors.

7. Shenandoah Garbage Collector

Introduced in Java 12, Shenandoah is also a concurrent garbage collector as the Z garbage collector and is also known for ultra-low pause time to reduce the overall GC pause times. G1 works to clear the heap memory only when the application is paused. Shenandoah on the other hand can relocate the objects without pausing the application execution. Garbage collection pauses are really frustrating to many users which can be avoided using these garbage collectors which include ZGC and Shenandoah. Although Shenandoah and ZGC both are advantageous for concurrent usage, the former one provides more tuning options.