mark_sweep() {
mark_phase()
sweep_phase()
}
mark_phase() {
for(r : roots) {
mark(r)
}
}
mark(obj) {
if(obj.mark == false)) {
obj.mark = true
for(child : children(obj)) {
mark(child)
}
}
}
sweep_phase() {
//loop the whole heap
sweeping = heap_start_addr
while(sweeping < heap_stop_addr) {
if(sweepping == true) {
//marked
sweeping.mark = false
} else {
//not marked, need to be free
free(sweeping);
}
//sweeping.size represent the object size
sweeping += sweeping.size;
}
}