Explain Mark and Sweep algorithm for Garbage Collector

There are two step – mark and sweep as name suggest.

Object has a mark bit at initial which is set to false (0).

In Mark stage – All REACHABLE object are marked as true and Unreachable still remain as false.

In Sweep stage – All Non Reachable (having mark bit as false) are cleared out from memory and remaining reachable object are marked as false again.

a)All the objects have their marked bits set to false at initial

GC mark and sweep
GC mark and sweep

 

b)Reachable objects are marked true

c)Non reachable objects are cleared from the heap. And rest reachable object are marked as false.

 

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *