Garbage collection algorithms in Java
One of Java’s key characteristics that has contributed to its success is garbage collection (GC). Java uses garbage collection as a way to deallocate unused memory. In essence, it involves finding every object that is still in use and designating the rest as trash. Since developers do not need to mark objects as being prepared for deallocation, Java’s garbage collection is seen as an automatic memory management model. Garbage collection is handled by low-priority threads.
You create objects by writing code and using frameworks to access their functionalities. It is not necessary for Java developers to deallocate memory or dereference objects. Garbage collector handles it automatically at the JVM level.
Usually, an algorithm has to stages.
· Mark :- The algorithm marks the unused objects from the heap which can be removed.
· Sweep :- Objects marked in the previous phase, are removed
Since the introduction of Java, there have been numerous modifications to the algorithms that operate behind the scenes to free memory. Let’s have a look at what they are.
1. Serial Garbage Collector
2. Parallel Garbage Collector
3. CMS Garbage Collector
4. G1 Garbage Collector
5. Z Garbage Collector
(Gupta, 2020)
Serial Garbage Collector
Due to the fact that it mostly relies on a single thread, this is the simplest GC implementation. This GC implementation stops all running application threads when it executes. Therefore, using it in multi-threaded applications like server environments is not a smart idea. The performance of the Serial Garbage Collector was the subject of an outstanding session delivered by Twitter engineers at QCon 2012, which is a useful approach to comprehend this collector.
Most programs that don’t require a lot of pause time and operate on client-style computers use the Serial GC as their garbage collector. We may use the following argument to activate the Serial Garbage Collector:
‘java -XX:+UseSerialGC -jar Application.java’
Parallel Garbage Collector
It is occasionally referred to as throughput collectors and serves as the JVM’s default GC. It utilizes several threads, as opposed to Serial Garbage Collector, to manage memory space, but it also pauses other application threads while running GC. By using this GC, we can control the maximum number of garbage collection threads, as well as the pause duration, throughput, and footprint (heap size) by using several command line arguments. They are:
· ‘-XX:ParallelGCThreads=N>’ :- can be used to regulate the number of garbage collector threads.
· ‘-XX:MaxGCPauseMillis=N>’ :- specifies the maximum pause time target.
· ‘-XX:GCTimeRatio=N>’ :- can be used to specify the maximum throughput objective, which is the ratio of time spent doing garbage collection to time spent otherwise.
Get Shehara Luvis’s stories in your inbox
Join Medium for free to get updates from this writer.
· ‘-XmxN>’ :- is used to specify how much heap memory a program will need to operate.
CMS Garbage Collector
Multiple garbage collector threads are used for garbage collection in the Concurrent Mark Sweep (CMS) implementation. It is made for programs that can tolerate sharing processor resources with the garbage collector while they are operating and want shorter trash collection pauses.
Simply said, programs that use this method of GC often react more slowly, but they don’t pause in the middle of a response to do garbage collection. Just a brief reminder that since this GC is concurrent, using System.gc() or another explicit garbage collection method will cause Concurrent Mode Failure / Interruption while the concurrent process is running.
The CMS collector will issue an OutOfMemoryError if more than 98 percent of the total time is used for CMS garbage collection and less than 2 percent of the heap is recovered. We may use the following flag to make the CMS Garbage Collector active:
‘java -XX:+UseParNewGC -jar Application.java’
G1 Garbage Collector
The G1 (Garbage First) Garbage Collector is made for programs that use multi-processor computers with a lot of memory. The CMS collector will be replaced with G1 collector because of its improved performance.
The G1 collector, in contrast to other collectors, divides the heap into a number of equal-sized heap areas, each of which is a continuous chunk of virtual memory. To detect the liveness of objects throughout the heap when executing garbage collections (i.e. phase 1, known as Marking)
Following the mark phase, G1 is aware of which zones are largely unoccupied. It first gathers in these locations, which typically results in a considerable quantity of open space (i.e. phase 2, known as Sweeping). This method of garbage collection is known as Garbage-First for this reason.
We may use the following argument to turn on the G1 Garbage Collector:
‘java -XX:+UseG1GC -jar Application.java’
Z Garbage Collector
ZGC (Z Garbage Collector) made its debut in Java 11 as an experimental option for Linux. It is a scalable low-latency garbage collector. ZGC was first made available for Windows and macOS by JDK 14. ZGC now has a production status for Java 15 and later.
ZGC is excellent for applications that demand low latency since it does all expensive work simultaneously without causing the execution of application threads to pause for no longer than 10ms. When the threads are active, it utilizes load barriers with colored pointers to carry out concurrent operations and to monitor heap utilization.
The fundamental idea behind ZGC is reference coloring or colored pointers. It implies that ZGC marks the object’s state using certain bits (metadata bits) of reference. Additionally, it manages heaps with sizes ranging from 8MB to 16TB. Additionally, pause times are not affected by the size of the heap, live set, or root set. Z Garbage Collector splits the heap similarly to G1, with the exception that heap regions might differ in size.
In JDK versions 15 and above, we may use the following option to enable the Z Garbage Collector:
‘java -XX:+UseZGC Application.java’
(baeldung, 2022)
References
baeldung, 2022. Baeldung. [Online]
Available at: https://www.baeldung.com/jvm-garbage-collectors
[Accessed 21 July 2022].
Gupta, L., 2020. HowToDoInJava. [Online]
Available at: https://howtodoinjava.com/java/garbage-collection/all-garbage-collection-algorithms/
[Accessed 21 July 2022].









