GC Algorithms in Java

Erandi Praboda
3 min readNov 14, 2020

Garbage collection is the process used to deallocate unused memory. The main objective of the garbage collection would be to find out the dead or abandoned objects and claim the memory space used by them. But during the period that the garbage collection is performed new objects get created, the garbage collector should have the capability to identify these recently created objects correctly, otherwise, the garbage collector will mistakenly identify these new objects as dead objects. One possibility of achieving this would be pausing the application for a certain amount of time. But could lead to a user experience problem since the application would be unresponsive during that period. Therefore Garbage Collection Algorithms should be designed to use a minimum application pausing time. The following are the available garbage collection algorithms to overcome these challenges.

Mark & Sweep Algorithm

Mark Phase: During this phase, the garbage collector will identify all the live objects. It keeps the track of specific fields called GC roots. GC roots can be the local variables of the currently executing methods or a static field of class which has been loaded; this should be an object reference not a primitive. Garbage collectors will start traversal from these references or GC roots available in the program and visit all the inner nodes which are referencing from the root in a Depth First Search (DFS) manner and mark object is live. Every object in the heap will keep track of whether it as live or not and this value is stored in a variable. The default value of this variable would be false and during the mark phase, it would set to true if the object is reachable.

Sweep Phase: All the objects which are not marked as the live object will be cleaned up and memory occupied by these objects will be reallocated.

As mentioned earlier when performing the Mark & Sweep Algorithm the application should be paused otherwise there may be some objects which are created after the mark phased and those objects also will be cleaned up during the sweep phase mistakenly by assuming that they are not reachable. This will leads to erroneous behavior.

Advantages of Mark & Sweep Algorithm

  • Algorithms will handle the cyclic references and will not leads to an infinite loop.

Disadvantages of Mark & Sweep Algorithm

  • After performing the mark and sweep there may be plenty of free regions in the memory but since they are fragmented there may be no single region large enough to accommodate a particular allocation and most of the allocations are still failing with an OutOfMemoryError.

Mark-Sweep-Compact Algorithm.

Similar to Mark & Sweep Algorithm fragmented memory area problems will be overcome by moving all live objects to the beginning of the memory region.

This will increase the application pausing time since it takes some time to move the objects after marking.

Mark and Copy Algorithmn.

Time is taken to move the objects to the beginning of the memory region can be reduced by doing it simultaneously with the marking phase this will achieve by Mark and Copy Algorithm. This will keep another memory region and while marking the objects as live they will be copied to the other region.

--

--