Garbage collection Algorithms

Garbage collection in Java means a memory management system that performs by java programs. In Java programs when objects are created in the heap it dedicates a portion of memory to that objects. There are some points where objects become unused or no longer needed. When these objects occur Garbage collector finds these objects and deletes them to free the memory.

Mark and Sweep

There are two steps that Garbage Collector follows when removing garbage.

  1. Mark — In this step garbage collector finds and identifies which objects are in use and which objects are no longer needed.
  2. Sweep- This step removes the objects that were identified in the first step and frees the memory.

Advantages

No need to manually handle the memory allocations or deallocations because memory is automatically handled by GC.

Automatic memory leak management

Disadvantages

Need more CPU power than the original application because JVM has to track each and every object. This can affect the performance of the requests which require large memory.

Some GC implementation can affect the program and can cause the program to stop unpredictably.

Serial Garbage Collector

This is the simplest garbage collection algorithm so far. This algorithm is most suitable for single-thread environments. It uses the only thread for garbage collection. That means this algorithm freezes all other application threads when this runs. Because this reason, should not be used in Multi-thread applications.

Parallel Garbage Collector

Parallel Garbage Collector is the default GC used by the JVM. Unlike The Serial Garbage Collector, we can use this for multiple threads environments to manage heap space. but it also freezes other application threads while performing GC. The only difference between serial and parallel garbage collector is that serial garbage collector uses a single thread for the garbage collection process while parallel garbage collector uses multiple threads for the garbage collection. If we use this GC algorithm, we can give specific maximum garbage collection threads and pause time, throughput, and heap size.

Concurrent Mark and Sweep (CMS) Garbage Collector

The Concurrent Mark Sweep (CMS) algorithm can use for multiple garbage collector threads for garbage collection. This algorithm does not freeze the threads in the application when collecting the garbage. Simply put, applications using this type of GC algorithm, respond slower on average, but do not stop responding to perform garbage collection. But this algorithm uses more CPU usage than other algorithms. We can use multiple CPUs for better application throughput. We should use a CMS garbage collector if we have more CPUs for use. Hence, it has an advantage over the parallel garbage collector. This collector also has a mode known as an incremental mode, which is being deprecated in Java SE 8 and may be removed in a future major release.

Reference

[1]Baeldung.com. [Online]. Available: https://www.baeldung.com/jvm-garbage-collectors#1-serial-garbage-collector. [Accessed: 10-Jul-2022].

[2]“Types of Garbage Collector in java,” www.javatpoint.com. [Online]. Available: https://www.javatpoint.com/types-of-garbage-collector-in-java. [Accessed: 10-Jul-2022].

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Resara Pamuditha

Resara Pamuditha

Associate Software Engineer at Virtusa