Garbage Collector: A Deep Dive
In Java, the Garbage Collector (GC) is one of the most important components. Gerrit Grunwald gave a talk about GC in June 2024 on JNation, with a very didactic way of understanding in depth how it works. Let’s see some highlights from his talk.
What is the Garbage Collector?
The Garbage Collector is a crucial component of Java's memory management system. Its primary purpose is to automatically identify and remove objects that are no longer needed by a program, thereby freeing up memory resources. This process helps prevent memory leaks and reduces the likelihood of the program running out of memory.
How many GCs exist?
There are many GCs, in the case of OpenJDK there are 6 GCs available. The Concurrent Mark and Sweep (CMS) does not exist anymore.
How does GC work?
It depends 😅, they have different algorithms and they separate the heap space.
Serial GC
Usage: Single-threaded applications.
Mechanism: Uses a single thread to perform all GC work, causing more stop-the-world (STW) pauses.
Parallel GC
Usage: Multi-threaded applications.
Mechanism: Uses multiple threads to speed up GC operations, reducing pause times compared to Serial GC.
Concurrent Mark-Sweep (CMS) GC
Usage: Applications requiring low pause times.
Mechanism: Performs most of the GC work concurrently with application threads to minimize pauses. However, it can suffer from fragmentation.
G1 (Garbage First) GC
Usage: General-purpose, large heap applications.
Mechanism: Divides the heap into regions and focuses on collecting regions with the most garbage first. Balances STW pauses and throughput.
Shenandoah GC
Usage: Low-latency applications.
Mechanism: Performs concurrent compaction along with concurrent marking and evacuation phases. This reduces pause times significantly, even for large heaps.
ZGC (Z Garbage Collector)
Usage: Ultra-low latency applications.
Mechanism: Designed for heaps ranging from small to very large (multi-terabyte). Performs all heavy lifting concurrently with the application, keeping pause times under 10ms.
In general, these are the mechanisms of how they perform the clean-up, however, each one of them performs different algorithms, having different phases and different latency due to STW.
If you want to understand more about how the phases work, I highly recommend Gerrit's talk, which you can access at the end of this article.
Which one should I use?
It also depends 😅, the best answer is to measure, and choose the one with the best result based on your application. However, the Microsoft team did an excellent job of determining the best GC based on some factors.
Conclusion
If you are a Java developer, you are using GC every day. I hope this article helped you understand a little more about how it works, and also how you can take advantage of selecting the best one for your application.
Maybe you can decrease the latency of your application only by selecting the right GC 😁
References
Jnation 2024 - Trash Talk: Exploring the memory management in the JVM
Microsoft Containerize your Java applications - https://learn.microsoft.com/en-us/azure/developer/java/containers/overview