Mark and sweep algorithm(garbage collector)

Preview:

DESCRIPTION

Mark and sweep algorithm(garbage collector)

Citation preview

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.

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!)

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

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.

5

Consider the following Consider the following Arrangement of nodesArrangement of nodes

Internally

foo bar baz

()

foo blarg

()

bar baz

()

XX

YY

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

47

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

Recommended