Chapter-13(Garbage Collection)

Chapter-13(Garbage Collection) - Garbage Collection garbage...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Garbage Collection
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
garbage collection : Heap-allocated records that are not reachable by any chain of pointers from program variables are garbage . The memory occupied by garbage should be reclaimed for use in allocating new records. This process is performed not by the compiler but by the runtime system (the support programs linked with the compiled code).
Background image of page 2
garbage collection let type list = { link: list, key: int } type tree = { key: int, left: tree, right:tree} function maketree ( ) = …. function showtree ( t:tree ) = …. . in let var x:= list ( link = nil, key = 7 } var y:= list ( link = x, key = 9 } in x.link := y end; let var p := maketree( ) var r := p.right var q := r.key in garbage-collect here showtree (r ) end end
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
13.1 MARK-AND-SWEEP COLLECTION Program variables and heap-allocated records form a directed graph. The variables are roots of this graph. A node n is reachable if there is a path of directed edges r → … → n starting at some root r . function DFS( x ) if x is a pointer into the heap if record x is not marked mark x for each field f i of record x DFS( x. f i )
Background image of page 4
13.1 MARK-AND-SWEEP COLLECTION Any node not marked must be garbage , and should be reclaimed. This can be done by a sweep of the entire heap garbage can be linked together in a linked list (the freelist ). The sweep phase should also unmark all the marked nodes, in preparation for the next garbage collection.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
After the garbage collection, the compiled program resumes execution. Whenever it wants to heap-allocate a new record, it gets a record from the freelist. When the freelist becomes empty, that is a good time to do another garbage collection to replenish the freelist. 13.1 MARK-AND-SWEEP COLLECTION
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
13.1 MARK-AND-SWEEP COLLECTION Cost of garbage collection : R: words of reachable data H: size of a heap Depth-first search takes time proportional to the number of nodes it marks.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

Chapter-13(Garbage Collection) - Garbage Collection garbage...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online