Compilation 2010 Garbage Collection Jan Midtgaard Michael I. Schwartzbach Aarhus University
The Garbage Collector A garbage collector is part of the runtime system It reclaims heap-allocated records (objects) that are no longer in use A garbage collector should : • reclaim all unused records • spend very little time per record • not cause significant delays • allow all of memory to be used These are difficult and conflicting requirements Garbage Collection 2
Life Without Garbage Collection Unused records must be explicitly deallocated This is superior if done correctly But it is easy to miss some records And it is dangerous to handle pointers Memory leaks in real life ( ical v.2.1 ): 35 MB 30 25 20 15 10 5 hours 0 Garbage Collection 3
Record Liveness Which records are still in use? Ideally, those that will be accessed in the future execution of the program But that is of course undecidable... Basic conservative approximation: A record is live if it is reachable from a stack location (local variable or local stack) Dead records may still point to each other Garbage Collection 4
A Heap With Live and Dead Records 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 5
The Mark-and-Sweep Algorithm Explore pointers starting from all stack locations and mark all the records encountered Sweep through all records in the heap and reclaim the unmarked ones Unmark all marked records Assumptions: • we know the start and size of each record in memory • we know which record fields are pointers • reclaimed records are kept in a freelist Garbage Collection 6
Pseudo Code for Mark-and-Sweep function DFS(x) { if (x is a heap pointer) if (x is not marked) { mark x; for (i=1; i<=|x|; i++) function Sweep() { DFS(x.f i ) p = first address in heap; } while (p<last address in heap) { } if (p is marked) unmark p; else { function Mark() { p.f 1 = freelist; foreach (v in a stack frame) freelist = p; DFS(v); } } p = next object pointer after p } } Garbage Collection 7
Marking and Sweeping (1/11) 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 8
Marking and Sweeping (2/11) 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 9
Marking and Sweeping (3/11) 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 10
Marking and Sweeping (4/11) 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 11
Marking and Sweeping (5/11) 00017 00008 15 12 p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 12
Marking and Sweeping (6/11) 00017 00008 15 12 freelist p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 13
Marking and Sweeping (6/11) 00017 00008 15 12 freelist p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 14
Marking and Sweeping (6/11) 00017 00008 15 12 freelist p 37 q 00042 00113 9 r 7 00249 20 37 00738 59 00371 Garbage Collection 15
Marking and Sweeping (7/11) 00017 00008 15 12 freelist p 37 q 00042 00113 r 7 00249 20 37 00738 59 00371 Garbage Collection 16
Marking and Sweeping (8/11) 00017 00008 15 12 freelist p 37 q 00042 00113 r 7 00249 20 37 00738 59 00371 Garbage Collection 17
Marking and Sweeping (9/11) 00017 00008 15 12 freelist p 37 q 00042 00113 r 7 00249 20 37 00738 59 00371 Garbage Collection 18
Marking and Sweeping (10/11) 00017 00008 15 12 freelist p 37 q 00042 00113 r 7 00249 20 37 00738 59 00371 Garbage Collection 19
Marking and Sweeping (11/11) 00017 00008 15 12 freelist p 37 q 00042 00113 r 7 00249 20 37 00738 59 00371 Garbage Collection 20
Analysis of Mark-and-Sweep Assume the heap has H words Assume that R words are reachable The cost of garbage collection is: c 1 R + c 2 H The cost per reclaimed word is: (c 1 R + c 2 H)/(H - R) If R is close to H, then this is expensive Garbage Collection 21
Allocation The freelist must be searched for a record that is large enough to provide the requested memory Free records may be sorted by size The freelist may become fragmented: containing many small free records but none that is large enough Defragmentation joins adjacent free records Garbage Collection 22
Pointer Reversal The DFS recursion stack could have size H It has at least size log (H) This may be too much (after all, memory is low) The recursion stack may be cleverly embedded in the fields of the marked records This technique makes mark-and-sweep practical Garbage Collection 23
The Reference Counting Algorithm Maintain a counter of the total number of references to each record For each assignment, update the counters A record is dead when its counter is zero Advantages: • catches dead records immediately • does not cause long pauses Disadvantages: • cannot detect cycles of dead records • is rather expensive Garbage Collection 24
Pseudo Code for Reference Counting function Increment(x) { x.count++; } function Decrement(x) { x.count--; if (x.count==0) PutOnFreeList(x); function PutOnFreelist(x) { } Decrement(x.f 1 ); x.f 1 = freelist; freelist = x; } function RemoveFromFreelist(x) { for (i=2; i<=|x|; i++) Decrement(x.f i ); } Garbage Collection 25
The Stop-and-Copy Algorithm Divide the heap space into two parts Only use one part at a time When it runs full, copy live records to the other part of the heap space Then switch the roles of the two parts Advantages: • fast allocation (no freelist) • avoids fragmentation Disadvantage: • wastes half your memory Garbage Collection 26
Before and After Stop-and-Copy 8 8 7 7 6 6 5 4 3 4 3 next 5 limit from-space to-space to-space from-space next limit Garbage Collection 27
Pseudo Code for Stop-and-Copy function Forward(x) { if (x from-space) { if (x.f 1 to-space) return x.f 1 ; else for (i=1; i<|x|; i++) next.f i = x.f i ; x.f 1 = next; next = next + sizeof(x); function Copy() { return x.f 1 ; scan = next = start of to-space; } else return x; foreach (v in a stack frame) } v = Forward(v); while (scan < next) { for (i=1; i<=|scan|; i++) scan.f i = Forward(scan.f i ); scan = scan + sizeof(scan); } } Garbage Collection 28
Stopping and Copying (1/13) 00017 00017 00008 15 15 12 p 37 00042 q 00113 9 r 7 00249 20 00738 37 59 00371 to-space from-space Garbage Collection 29
Stopping and Copying (2/13) 09000 00017 00017 00008 15 15 15 12 p 37 00042 q 00113 9 r 7 00249 20 00738 37 59 00371 to-space from-space Garbage Collection 30
Stopping and Copying (3/13) 09000 00017 00017 00008 15 15 15 12 p 37 00042 q 00113 9 r 7 00249 20 00738 37 59 00371 to-space from-space Garbage Collection 31
Stopping and Copying (4/13) 09000 00017 00017 00008 15 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 20 00738 37 59 00371 to-space from-space Garbage Collection 32
Stopping and Copying (5/13) 09000 00017 00017 00008 15 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 20 00738 37 59 00371 to-space from-space Garbage Collection 33
Stopping and Copying (6/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 20 00738 37 59 00371 to-space from-space Garbage Collection 34
Stopping and Copying (7/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 20 00738 37 59 00371 to-space from-space Garbage Collection 35
Stopping and Copying (8/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 20 00738 37 59 00371 to-space from-space Garbage Collection 36
Stopping and Copying (9/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 00249 20 20 00738 37 59 00371 to-space from-space Garbage Collection 37
Stopping and Copying (10/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 00936 20 20 00738 37 59 00371 to-space from-space Garbage Collection 38
Stopping and Copying (11/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 00936 20 20 00738 37 00948 59 59 00371 to-space from-space Garbage Collection 39
Stopping and Copying (12/13) 09000 09024 00017 00017 00008 15 12 15 15 12 p 37 00042 q 00113 9 r 09012 7 00249 37 00936 20 20 00738 37 00948 59 59 00371 to-space from-space Garbage Collection 40
Stopping and Copying (13/13) 09000 09024 15 12 p 37 q r 09012 37 00936 20 00948 59 from-space to-space Garbage Collection 41
Recommend
More recommend