How do we mark reachable objects? Disadvantages of mark-sweep GC - - PowerPoint PPT Presentation

how do we mark reachable objects disadvantages of mark
SMART_READER_LITE
LIVE PREVIEW

How do we mark reachable objects? Disadvantages of mark-sweep GC - - PowerPoint PPT Presentation

How do we mark reachable objects? Disadvantages of mark-sweep GC Stop-the-world algorithm Computation suspended while GC runs Pause time may be high Not practical for real-time, interactive applications, video games High cost:


slide-1
SLIDE 1

How do we mark reachable objects?

slide-2
SLIDE 2

Disadvantages of mark-sweep GC

 Stop-the-world algorithm

 Computation suspended while GC runs  Pause time may be high

 Not practical for real-time, interactive applications, video

games

 High cost:

 proportional to size of heap (not just live objects)  Why?

 Active objects visited by mark phase  All of memory visited by sweep phase

2

slide-3
SLIDE 3

Mark-sweep algorithm

// The mark-sweep collector mark_sweep() { for R in Roots mark(*R) sweep() if free_pool is empty abort "Memory exhausted" } // Simple recursive marking mark(N) { if mark_bit(N) == unmarked mark_bit(N) = marked for M in Children(N) mark(*M) }

3

// The eager sweep of the heap sweep() { N = Heap_bottom while N < Heap_top if mark_bit(N) == unmarked free(N) else mark_bit(N) = unmarked N = N + size(N) }

slide-4
SLIDE 4

How can we improve marking?

 Using a marking stack

 Problem:

 Recursion may cause system stack to overflow  Procedure overhead: both time and space

 Solutions:

 Replace recursive calls with iterative loops  Use auxiliary data structures (e.g., a stack data structure)

 Stack holds pointers to objects known to be live  Unmarked children marked, pushed on stack if have pointers  Objects without pointers only marked  Marking phase terminates when stack is empty

4

slide-5
SLIDE 5

Marking stack

 Maximum depth of stack

 Depends on longest path through graph that has to be

marked

 In most systems stacks are generally shallow

 Safe GC must be able to handle exceptional cases

 May need to minimize stack depth

5

slide-6
SLIDE 6

Iterative Marking

6

mark_heap() { mark_stack = empty for R in Roots mark_bit(*R) = marked push(*R, mark_stack) mark() } mark() { while mark_stack != empty N = pop(mark_stack) for M in Children(N) if mark_bit(*M) == unmarked mark_bit(*M) = marked if not atom(*M) push(*M, mark_stack) } Marking with resumption stack

slide-7
SLIDE 7

Minimizing stack depth

 Why is this important?

 Stack can overflow  Whys is GC needed?

 What if the GC runs out of storage?

 How can it be done?

 push constituent pointers of large objects in small

groups onto the stack (Boehm-Demers-Weiser)

 Using pointer reversal

7

slide-8
SLIDE 8

Addressing stack overflow

 Marking stack can make detection easier and recovery

action taken

 Check in each push operation ($$$$$$)  Single check by counting # of pointers in each popped

node

 Use guard page (if OS support)

 Read-only page. Cannot push unto guard page

 How to handle stack overflow?

 Knuth’s approach  Kurokawa’s approach

8

slide-9
SLIDE 9

Knuth proposed in 1973

 Treat marking stack circularly

for R in Roots push(*R, new_roots);

  • verflow = false;

while true

  • verflow = cyclic_stack_mark(new_roots);

if overflow == true new_roots = scan_heap(); else break;

 scan_heap returns marked nodes pointing to

unmarked nodes

9

slide-10
SLIDE 10

Kurokawa proposal in 1981

 On overflow run Stacked Node Checking algorithm  remove items from stack that have fewer than 2

unmarked children

 no child is unmarked: clear slot  one child is unmarked: replace slot entry by a

descendent with 2 or more unmarked children marking the passed ones

 Not robust

 Possible that no additional space will be found on the

stack

10

slide-11
SLIDE 11

Pointer reversal

 Eliminate need for marking stack

 Push stack in heap nodes  Maintains 3 variables: previous, current, and next

 Any efficient marking process must record the branch-

points it passed

 Temporarily reversing of pointers traversed by mark

 child-pointers become ancestor-pointers

 restore pointer fields when tracing back  developed independently by Schorr and Waite (1967)

and by Deutsch (1973)

11

slide-12
SLIDE 12

Pointer reversal

advance retreat switch enter unmarked atom or marked internal node

  • f sub-graph

head of graph head of sub-graph DFA for binary tree structures

12

slide-13
SLIDE 13

Pointer reversal (advance phase)

previous current

13

slide-14
SLIDE 14

Pointer reversal (advance phase)

previous current

14

slide-15
SLIDE 15

Pointer reversal (advance phase)

previous current next

15

slide-16
SLIDE 16

Pointer reversal (advance phase)

previous current next

16

slide-17
SLIDE 17

Pointer reversal (advance phase)

previous current next

17

slide-18
SLIDE 18

Pointer reversal (advance phase)

previous current next

18

slide-19
SLIDE 19

Pointer reversal (switch phase)

previous current next

19

slide-20
SLIDE 20

Pointer reversal (switch phase)

previous current next

20

slide-21
SLIDE 21

Pointer reversal (switch phase)

previous current next

21

slide-22
SLIDE 22

Pointer reversal (switch phase)

previous current next

22

slide-23
SLIDE 23

Pointer reversal (switch phase)

previous current next

23

slide-24
SLIDE 24

Pointer reversal (switch phase)

previous current next

24

slide-25
SLIDE 25

Pointer reversal (retreat phase)

previous current next

25

slide-26
SLIDE 26

Pointer reversal (retreat phase)

previous current next

26

slide-27
SLIDE 27

Pointer reversal (retreat phase)

previous current next

27

slide-28
SLIDE 28

Pointer reversal (retreat phase)

previous current next

28

slide-29
SLIDE 29

Pointer reversal (retreat phase)

previous current next

29