Garbage Collection In Java

What is Garbage Collection?

Garbage collection is a mechanism that is used to deallocate unused memory. Basically, it is the process of tracking down all the objects that are currently being used; all other objects are considered as garbage. This tracking of the objects is done by something called reference counting. This reference counting mechanism keeps track of the objects and once the object is inaccessible via reference, the garbage collector automatically reclaims that memory. Because of this, it removes the burden of memory management for objects from the users. Usually, garbage collection runs automatically on low-priority threads.

So, why do we need to be aware of how the garbage collector works? Why should we bother about it?

One may ask this question. The answer is, the garbage collector is doing the memory management for you and memory management is a part of your application. In short, if you know how the garbage collector works, you can write optimized code, especially when it comes to Java, you can configure the pattern of the garbage collector. Choosing the correct garbage collection algorithm for your application means you completely understand what your application is doing. You get my point, right?
If you want to know about .NET garbage collection, please refer to my previous articles on the following links.

.NET Garbage collection

It uses a generational approach. Please read my previous posts for a detailed explanation on this.

Java Garbage collection

Unlike the generational approach in .NET CLR, in Java, there are 4 GC algorithm types (as of Java 9). 
  1. Serial GC
  2. Parallel GC
  3. Concurrent Mark & Sweep GC (or "CMS")
  4. Garbage First (G1) GC
I will take each of them one by one and explain to you. So, you will get a better idea.
Serial GC
It uses a single thread to perform all garbage collection work, which makes it relatively efficient because there is no communication overhead between threads. It is best suited to single CPU machines. This might be a good solution if you are running your application in a docker container where each JVM might have limited resources to use with.
Parallel GC
Performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multithreaded hardware.
Performs most of its work concurrently to keep garbage collection pauses short. it attempts to do GC (or at least most of the garbage collection was done) while the application is still running. It is designed for applications using relatively large data (memory allocation) in which response time is more important than overall throughput because the techniques used to minimize pauses can reduce application performance.
This is the latest innovation from Oracle. The goal of this algorithm is to reduce GC pause time and increase throughput. Garbage-First collector divides the whole heap into multiple equally sized areas and runs a ‘mark’ phase across the entire heap. By doing this, it identifies the areas that are filled mostly with objects that can be collected(places where can free a large number of memories), that's where the garbage first name has come for it. Also, this has been performed parallel while other application threads are allowed to run(avoiding “stop the world”), which means largely reduced pause time.
Here are good explanations about G1 GC:

Which algorithm should I choose?

My opinion is to let the JVM select the right algorithm. But if you want to override, I hope you know what you are doing.
Algorithm In Case Of Command-Line Option
Serial GC Running JVM in a single CPU environment  -XX:+UseSerialGC
Parallel GC Throughput is the most important -XX:+UseParallelGC
CMS GC The application needs more responsive than overall throughput -XX:+UseConcMarkSweepGC
G1 GC Multi CPU machines with a large number of memories (More suitable for heap sizes > 5GB) -XX:+UseG1GC
Identifying the proper garbage collector algorithm is a self-tuning thing done by JVM itself. This is called ergonomics.

Where is Java headed?

As you may already have noticed Java Garbage collection has evolved much over a few years compared to the traditional approach used during the early decades. Still, at the moment of writing this article, research is going on for providing the best-optimized garbage collector for the users. As a result of that G1 is the current default garbage collector for Java since v9 onwards. And Z GC is supposed to be the future since v11 onwards.
  • Java 7 >> Parallel GC
  • Java 8 >> Parallel GC
  • Java 9 >> G1 GC
  • Java 10 >> G1 GC
  • Java 11 >> Z GC?


In this article, we studied about Garbage Collection in Java.