47
1 Mark – Sweep Algorithm Mark – Sweep Algorithm first algorithm for automatic first algorithm for automatic storage reclamation (McCarthy storage reclamation (McCarthy 1960). 1960). a stop and run algorithm. a stop and run algorithm. tracing garbage collection tracing garbage collection technique technique works in 2 Phases: works in 2 Phases: mark all live nodes by traversal. mark all live nodes by traversal. sweep the heap by a linear scan. sweep the heap by a linear scan.

Mark and sweep algorithm(garbage collector)

Embed Size (px)

DESCRIPTION

Mark and sweep algorithm(garbage collector)

Citation preview

Page 1: Mark and sweep algorithm(garbage collector)

1

Mark – Sweep AlgorithmMark – Sweep Algorithm

first algorithm for automatic storage first algorithm for automatic storage reclamation (McCarthy 1960).reclamation (McCarthy 1960).

a stop and run algorithm.a stop and run algorithm. tracing garbage collection techniquetracing garbage collection technique works in 2 Phases:works in 2 Phases:

– mark all live nodes by traversal.mark all live nodes by traversal.

– sweep the heap by a linear scan.sweep the heap by a linear scan.

Page 2: Mark and sweep algorithm(garbage collector)

2

Mark-Sweep AlgorithmMark-Sweep AlgorithmBenefitsBenefits::

a)a) No overhead on pointer manipulations.No overhead on pointer manipulations.

b)b) Low space cost: using a simple mark-Low space cost: using a simple mark-field/bit.field/bit.

DrawbacksDrawbacks::

a)a) Computation halted while gcComputation halted while gc

b)b) Every cell is visited during marking Every cell is visited during marking

c)c) Cells are again re-examined by sweepCells are again re-examined by sweep

d)d) Recursive marking (time and space!)Recursive marking (time and space!)

Page 3: Mark and sweep algorithm(garbage collector)

3

Mark -- Sweep AlgorithmMark -- Sweep Algorithm Each block or its corresponding book keeping Each block or its corresponding book keeping

must contain bit (mark bit)must contain bit (mark bit) Initially all blocks are unmarkedInitially all blocks are unmarked Starting at each symbol perform a search marking Starting at each symbol perform a search marking

all blocks reachable (mark means in-use)all blocks reachable (mark means in-use) Sweep through all blocks.Sweep through all blocks.

– If marked: UnmarkIf marked: Unmark

– If unmarked: move to free list ( i.e free it )If unmarked: move to free list ( i.e free it )

Note: Algorithm must be only thing runningNote: Algorithm must be only thing running Garbage collection is only done when necessaryGarbage collection is only done when necessary

– i.e. When free list is emptyi.e. When free list is empty

Page 4: Mark and sweep algorithm(garbage collector)

4

Characteristics of Mark and Sweep Characteristics of Mark and Sweep AlgorithmAlgorithm

Can place an upper bound on the total amount of Can place an upper bound on the total amount of time required to perform complete GC. Use this time required to perform complete GC. Use this to pace GC against ongoing allocation needs.to pace GC against ongoing allocation needs.

No “time intrusive” overheads to interfere with No “time intrusive” overheads to interfere with calculation of worst-case task execution times.calculation of worst-case task execution times.

But cannot guarantee to avoid fragmentation.But cannot guarantee to avoid fragmentation.

Page 5: Mark and sweep algorithm(garbage collector)

5

Consider the following Consider the following Arrangement of nodesArrangement of nodes

Internally

foo bar baz

()

foo blarg

()

bar baz

()

XX

YY

Page 6: Mark and sweep algorithm(garbage collector)

6

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 7: Mark and sweep algorithm(garbage collector)

7

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 8: Mark and sweep algorithm(garbage collector)

8

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 9: Mark and sweep algorithm(garbage collector)

9

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 10: Mark and sweep algorithm(garbage collector)

10

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 11: Mark and sweep algorithm(garbage collector)

11

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 12: Mark and sweep algorithm(garbage collector)

12

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 13: Mark and sweep algorithm(garbage collector)

13

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 14: Mark and sweep algorithm(garbage collector)

14

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 15: Mark and sweep algorithm(garbage collector)

15

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 16: Mark and sweep algorithm(garbage collector)

16

Free ListFree List

XX

()

()

YY

()

FreeFree

()

MarkMark

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 17: Mark and sweep algorithm(garbage collector)

17

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 18: Mark and sweep algorithm(garbage collector)

18

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 19: Mark and sweep algorithm(garbage collector)

19

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 20: Mark and sweep algorithm(garbage collector)

20

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 21: Mark and sweep algorithm(garbage collector)

21

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 22: Mark and sweep algorithm(garbage collector)

22

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 23: Mark and sweep algorithm(garbage collector)

23

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 24: Mark and sweep algorithm(garbage collector)

24

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 25: Mark and sweep algorithm(garbage collector)

25

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 26: Mark and sweep algorithm(garbage collector)

26

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 27: Mark and sweep algorithm(garbage collector)

27

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 28: Mark and sweep algorithm(garbage collector)

28

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 29: Mark and sweep algorithm(garbage collector)

29

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 30: Mark and sweep algorithm(garbage collector)

30

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 31: Mark and sweep algorithm(garbage collector)

31

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 32: Mark and sweep algorithm(garbage collector)

32

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 33: Mark and sweep algorithm(garbage collector)

33

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 34: Mark and sweep algorithm(garbage collector)

34

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 35: Mark and sweep algorithm(garbage collector)

35

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 36: Mark and sweep algorithm(garbage collector)

36

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 37: Mark and sweep algorithm(garbage collector)

37

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 38: Mark and sweep algorithm(garbage collector)

38

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 39: Mark and sweep algorithm(garbage collector)

39

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 40: Mark and sweep algorithm(garbage collector)

40

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 41: Mark and sweep algorithm(garbage collector)

41

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 42: Mark and sweep algorithm(garbage collector)

42

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 43: Mark and sweep algorithm(garbage collector)

43

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 44: Mark and sweep algorithm(garbage collector)

44

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 45: Mark and sweep algorithm(garbage collector)

45

Free ListFree List

XX

()

()

YY

()

FreeFreeSweepSweep

()

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 46: Mark and sweep algorithm(garbage collector)

46

Free ListFree List

XX

()

()

YY

()

FreeFreeDoneDone

()

Here , X and Y correspond to the root Pointers and the block pointed to by “Free” Corresponds to the Free List.

Page 47: Mark and sweep algorithm(garbage collector)

47

As Simple as ......As Simple as ......