Upcoming SlideShare
×

# Mark and sweep algorithm(garbage collector)

6,270 views

Published on

Mark and sweep algorithm(garbage collector)

Published in: Education
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
6,270
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
117
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Mark and sweep algorithm(garbage collector)

1. 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. 2. Mark-Sweep AlgorithmBenefits:a) No overhead on pointer manipulations.b) Low space cost: using a simple mark- field/bit.Drawbacks:a) Computation halted while gcb) Every cell is visited during markingc) Cells are again re-examined by sweepd) Recursive marking (time and space!) 2
3. 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. 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
5. 5. Consider the followingArrangement of nodes Internally X () foo blarg () bar baz Y () foo bar baz 5
6. 6. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 6
7. 7. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 7
8. 8. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 8
9. 9. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 9
10. 10. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 10
11. 11. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 11
12. 12. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 12
13. 13. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 13
14. 14. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 14
15. 15. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 15
16. 16. Free Mark Free ListX () ()Y () () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 16
17. 17. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 17
18. 18. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 18
19. 19. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 19
20. 20. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 20
21. 21. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 21
22. 22. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 22
23. 23. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 23
24. 24. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 24
25. 25. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 25
26. 26. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 26
27. 27. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 27
28. 28. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 28
29. 29. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 29
30. 30. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 30
31. 31. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 31
32. 32. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 32
33. 33. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 33
34. 34. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 34
35. 35. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 35
36. 36. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 36
37. 37. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 37
38. 38. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 38
39. 39. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 39
40. 40. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 40
41. 41. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 41
42. 42. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 42
43. 43. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 43
44. 44. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. 44
45. 45. Sweep Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. () 45
46. 46. Done Free Free ListX () ()Y () Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List. () 46
47. 47. As Simple as ...... 47