Mark-and-Sweep Garbage Collection Algorithm in Java

Garbage collection algorithms, such as mark and sweep, run in the background to manage memory in programming languages like C++ and Java. When objects are created dynamically, they occupy memory in the heap. However, if we keep creating objects without freeing memory, it can lead to Out Of Memory errors. To prevent this, garbage collection automatically releases memory for objects no longer referenced or unreachable in the program. It alleviates the burden of memory management from the programmer, as the garbage collector deallocates memory for unreferenced objects, ensuring available space for new objects to be allocated.

Mark and Sweep Algorithm

The mark-and-sweep garbage collection algorithm is a simple and efficient algorithm used to reclaim memory no longer being used by an application. The algorithm works by first marking all objects that are still in use. Once all of the live objects have been marked, the garbage collector sweeps through the heap and reclaims all the unmarked objects.

1. Marking phase: When an object is created, the mark bit is set to 0(false). The marking phase is responsible for identifying all of the live objects in the heap. It is done by traversing the heap and marking all of the objects that are reachable from the roots of the heap. The heap's roots are the objects directly accessible to the application, such as local variables and object references.

The root variable is a variable that refers to an object that is accessible from the local variable. We assume that there is only one root.

Algorithm

2. Sweeping phase: The sweeping phase is responsible for reclaiming all unmarked objects in the heap. It is done by freeing the memory allocated to the unmarked objects.

Algorithm:

The mark-and-sweep algorithm is called a "tracing garbage collector" because it systematically traces and identifies all objects that can be reached directly or indirectly from the program's root references.

Example:

  1. All objects have their marked bits initially set to false.
    Mark-and-Sweep Garbage Collection Algorithm in Java
  2. Objects that are reachable are marked as true.
    Mark-and-Sweep Garbage Collection Algorithm in Java
  3. Nonreachable objects are removed or cleared from the heap during the garbage collection process. Mark-and-Sweep Garbage Collection Algorithm in Java

Advantages of Mark and Sweep Algorithm

  • The Mark and Sweep algorithm handles cyclic references without getting stuck in an infinite loop. It ensures that the algorithm can correctly identify reachable objects even in cycles.
  • The algorithm does not introduce additional overheads during its execution, making it an efficient and reliable approach for garbage collection.

Disadvantages of Mark and Sweep Algorithm

  • One of the significant drawbacks of the mark-and-sweep approach is that it causes interruptions in normal program execution while the garbage collection algorithm is running.
  • Another disadvantage of the mark-and-sweep garbage collection algorithm is that, after it is run several times on a program, the reachable objects become fragmented, with many small unused memory regions between them.
Mark-and-Sweep Garbage Collection Algorithm in Java

The green squares in the image represent objects that are reachable from the roots. The white squares represent objects that are not reachable from the roots.

Fragmentation is a problem that occurs when small, unused memory regions are scattered throughout the heap. It can happen when the garbage collector reclaims memory from no longer-needed objects. If the garbage collector does not compact the heap, these small, unused memory regions can prevent allocating new objects.

Compaction is rearranging the objects in the heap so that the free memory regions are contiguous. It can be done by moving the objects around in the heap or copying them to a new location.

Compaction can reduce fragmentation and improve the performance of the garbage collector. The garbage collector can quickly find free memory regions when the heap is compacted.

In the example you provided, five free memory regions of sizes 1, 1, 2, 3, and 5 units exist. These free memory regions are fragmented, meaning that they are not contiguous. It prevents the allocation of an object that requires 10 units of memory.

If the heap was compacted, the five free memory regions would be combined into a single free memory region of size 12 units. It would allow the allocation of the object that requires 10 memory units.

Compaction can be time-consuming, so it is only sometimes performed by the garbage collector. However, compaction can be beneficial for applications that experience frequent fragmentation.