Garbage Collection Algorithm — Mark & Sweep

Background

Memory Leak fixed

Garbage Collection

Mark & Sweep Algorithm

Mark Phase

After Completion of Mark Phase

Sweep phase

After completion of Sweep Phase
// Pseudo Code for Mark 
Mark(root)
If root.marked = false then
root.marked = true
For each v root.references()
Mark(v)
Sweep()
For each object in heap
If object.marked = true then
object.marked = false
else
heap.release(object)

Advantages of Mark & Sweep

Disadvantages of Mark & Sweep

Senior Software Engineer @Microsoft. Writes about Distributed Systems, Programming Languages & Tech Interviews

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