1. Mark – Sweep Algorithm
first algorithm for automatic storage
reclamation (McCarthy 1960).
a stop and run algorithm.
tracing garbage collection technique
works in 2 Phases:
– mark all live nodes by traversal.
– sweep the heap by a linear scan.
1
2. Mark-Sweep Algorithm
Benefits:
a) No overhead on pointer manipulations.
b) Low space cost: using a simple mark-
field/bit.
Drawbacks:
a) Computation halted while gc
b) Every cell is visited during marking
c) Cells are again re-examined by sweep
d) Recursive marking (time and space!)
2
3. Mark -- Sweep Algorithm
Each block or its corresponding book keeping
must contain bit (mark bit)
Initially all blocks are unmarked
Starting at each symbol perform a search marking
all blocks reachable (mark means in-use)
Sweep through all blocks.
– If marked: Unmark
– If unmarked: move to free list ( i.e free it )
Note: Algorithm must be only thing running
Garbage collection is only done when necessary
– i.e. When free list is empty
3
4. Characteristics of Mark and Sweep
Algorithm
Can place an upper bound on the total amount of
time required to perform complete GC. Use this
to pace GC against ongoing allocation needs.
No “time intrusive” overheads to interfere with
calculation of worst-case task execution times.
But cannot guarantee to avoid fragmentation.
4
6. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
6
7. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
7
8. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
8
9. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
9
10. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
10
11. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
11
12. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
12
13. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
13
14. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
14
15. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
15
16. Free
Mark
Free List
X
() ()
Y
() () Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
16
17. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
17
18. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
18
19. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
19
20. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
20
21. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
21
22. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
22
23. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
23
24. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
24
25. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
25
26. Sweep
Free
Free List
X
() ()
Y
()
Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
26
27. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
27
28. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
28
29. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
29
30. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
30
31. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
31
32. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
32
33. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
33
34. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
34
35. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
35
36. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
36
37. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
37
38. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
38
39. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
39
40. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
40
41. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
41
42. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
42
43. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
43
44. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
44
45. Sweep
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
() 45
46. Done
Free
Free List
X
() ()
Y
() Here , X and Y correspond to
the root Pointers and the
block pointed to by “Free”
Corresponds to the Free List.
() 46