Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.

Slides:



Advertisements
Similar presentations
Garbage collection (& Midterm Topics) David Walker COS 320.
Advertisements

Part IV: Memory Management
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
1 Overview Assignment 5: hints  Garbage collection Assignment 4: solution.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Copyright, 1996 © Dale Carnegie & Associates, Inc. Mark-Sweep A tracing garbage collection technique Hagen Böhm November 21st, 2001
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CS 536 Spring Automatic Memory Management Lecture 24.
An Efficient Machine-Independent Procedure for Garbage Collection in Various List Structures, Schorr and Waite CACM August 1967, pp Curtis Dunham.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Garbage Collection Mooly Sagiv html://
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Fast, Effective Code Generation in a Just-In-Time Java Compiler Rejin P. James & Roshan C. Subudhi CSE Department USC, Columbia.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Fast Conservative Garbage Collection Rifat Shahriyar Stephen M. Blackburn Australian National University Kathryn S. M cKinley Microsoft Research.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
Comparison of Compacting Algorithms for Garbage Collection Mrinal Deo CS395T – Spring
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Memory Management -Memory allocation -Garbage collection.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Simple Generational GC Andrew W. Appel (Practice and Experience, February 1989) Rudy Kaplan Depena CS 395T: Memory Management February 9, 2009.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
LINKED LISTS.
Garbage Collection What is garbage and how can we deal with it?
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Concepts of programming languages
Automatic Memory Management
Strategies for automatic memory management
Closure Representations in Higher-Order Programming Languages
Topic 3-b Run-Time Environment
Chapter 12 Memory Management
CS703 - Advanced Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

Garbage collection David Walker CS 320

Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference counting (Appel 13.2) –Copying collection (Appel 13.3) –Generational collection (Appel 13.4) –Baker’s algorithm (Appel 13.6) Today: –Mark-sweep collection (Appel 13.1) –Conservative collection –Compiler interface (13.7)

Mark-sweep A two-phase algorithm –Mark phase: Depth first traversal of object graph from the roots to mark live data –Sweep phase: iterate over entire heap, adding the unmarked data back onto the free list

Example Free list r1 In use On free list

Example Free list r1 In use On free list Marked Mark Phase: mark nodes reachable from roots

Example Free list r1 In use On free list Marked Mark Phase: mark nodes reachable from roots

Example Free list r1 In use On free list Marked Mark Phase: mark nodes reachable from roots

Example Free list r1 In use On free list Marked Sweep Phase: set up sweep pointer; begin sweep p

Example Free list r1 In use On free list Marked Sweep Phase: add unmarked blocks to free list p

Example Free list r1 In use On free list Marked Sweep Phase p

Example Free list r1 In use On free list Marked Sweep Phase: retain & unmark marked blocks p

Example Free list r1 In use On free list Marked Sweep Phase p

Example Free list r1 In use On free list Marked Sweep Phase: GC complete when heap boundary encountered; resume program p

Cost of Mark Sweep Cost of mark phase: –O(R) where R is the # of reachable words –Assume cost is c1 * R (c1 may be 10 instr’s) Cost of sweep phase: –O(H) where H is the # of words in entire heap –Assume cost is c2 * H (c2 may be 3 instr’s) Amortized analysis –Each collection returns H - R words –For every allocated word, we have GC cost: ((c1 * R) + (c2 * H)) / (H - R) –R / H must be sufficiently small or GC cost is high –Eg: if R / H is larger than.5, increase heap size

A Hidden Cost Depth-first search is usually implemented as a recursive algorithm –Uses stack space proportional to the longest path in the graph of reachable objects one activation record/node in the path activation records are big –If the heap is one long linked list, the stack space used in the algorithm will be greater than the heap size!! –What do we do?

A nifty trick Deutsch-Schorr-Waite pointer reversal –Rather using a recursive algorithm, reuse the components of the graph you are traversing to build an explicit stack –This implementation trick only demands a few extra bits/block rather than an entire activation record/block –We already needed a few extra bits per block to hold the “mark” anyway

DSW Algorithm … backnext

DSW Algorithm … backnext … back next

DSW Algorithm … backnext … back next … back next

DSW Algorithm … backnext … back next … back next … back next

DSW Algorithm … backnext … back next … back next … back next extra bits needed to keep track of which record fields we have processed so far

DSW Setup Extra space required for sweep: –1 bit/record to keep track of whether the record has been seen (the “mark bit”) –f log 2 bits/record where f is the number of fields in the record to keep track of how many fields have been processed assume a vector: done[x] Functions: –mark x = sets x’s mark bit –marked x = true if x’s mark bit is set –pointer x = true if x is a pointer –fields x = returns number of fields in the record x

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) (* depth-first search in constant space *) (* next is object being processed *) (* done[next] is field being processed *)

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) back = nil; mark next; done[next] = 0; (* depth-first search in constant space *)

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) y = next.i if (pointer y) & not (marked y) then next.i = back; back = next; next = y; mark next; done[next] = 0; else done[next] = i + 1 (* depth-first search in constant space *) reuse field to store back ptr

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) y = next.i if (pointer y) & not (marked y) then next.i = back; back = next; next = y; mark next; done[next] = 0; else done[next] = i + 1 (* depth-first search in constant space *) initialize for next iteration

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) y = next.i if (pointer y) & not (marked y) then next.i = back; back = next; next = y; mark next; done[next] = 0; else done[next] = i + 1 (* depth-first search in constant space *) field is done

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) y = next; next = back; if next = nil then return; i = done[next]; back = next.i; next.i = y; done[next] = i + 1; (* depth-first search in constant space *) dfs complete

DSW Algorithm fun dfs(next) = if (pointer next) & not (marked next) then (* initialization *) while true do i = done[next] if i < (fields next) then (* process ith field *) else (* back-track to previous record *) y = next; next = back; if next = nil then return; i = done[next]; back = next.i; next.i = y; done[next] = i + 1; (* depth-first search in constant space *) advance to next field

More Mark-Sweep Mark-sweep collectors can benefit from the tricks used to implement malloc/free efficiently –multiple free lists, one size of block/list Mark-sweep can suffer from fragmentation –blocks not copied and compacted like in copying collection Mark-sweep doesn’t require 2x live data size to operate –but if the ratio of live data to heap size is too large then performance suffers

Conservative Collection Even languages like C can benefit from GC –Boehm-Weiser-Demers conservative GC uses heuristics to determine which objects are pointers and which are integers without any language support last 2 bits are non-zero => can’t be a pointer integer is not in allocated heap range => can’t be a pointer mark phase traverses all possible pointers conservative because it may retain data that isn’t reachable –thinks an integer is actually a pointer all gc is conservative anyway so this is almost never an issue (despite what people say) sound if your program doesn’t manufacture pointers from integers by, say, using xor (using normal pointer arithmetic is fine)

Compiler Interface The interface to the garbage collector involves two main parts –allocation code languages can allocated up to approx 1 word/7 instructions allocation code must be blazingly fast! should be inlined and optimized to avoid call-return overhead –gc code to call gc code, the program must identify the roots to traverse data, heap layout must be specified somehow

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into function result 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move result into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1]

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into function result 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move result into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1] useful computation not alloc overhead

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into function result 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move result into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1] inline alloc code

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into computationally useful place 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move next into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1] combine moves

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into computationally useful place 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move next into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1] eliminate useless store

Allocation Code Assume size of record allocated is N: 1.Call alloc function 2.Test next + N < limit (call gc on failure) 3.Move next into computationally useful place 4.Clear M[next],..., M[next + N – 1] 5.next = next + N 6.Return from alloc function 7.Move next into computationally useful place 8.Store useful values into M[next],....,M[next + N - 1] total overhead for allocation on the order of 3 instructions/alloc

Calling GC code To call the GC, program must: –identify the roots: a GC-point, is an control-flow point where the garbage collector may be called –allocation point; function call for any GC-point, compiler generates a pointer map that says which registers, stack locations in the current frame contain pointers a global table maps GC-points (code addresses) to pointer maps when program calls the GC, to find all roots: –GC scans down stack, one activation record at a time, looking up the current pointer map for that record

Calling GC code To call the GC, program must: –enable GC to determine data layout of all objects in the heap for ML, Tiger, Pascal: –every record has a header with size and pointer info for Java, Modula-3: –each object has an extra field that points to class definition –gc uses class definition to determine object layout including size and pointer info

Summary Garbage collectors are a complex and fascinating part of any modern language implementation Different collection algs have pros/cons –explicit MM, reference counting, copying, generational, mark-sweep –all methods, including explicit MM have costs –optimizations make allocation fast, GC time, space and latency requirements acceptable –read Appel Chapter 13 and be able to analyze, compare and contrast different GC mechanisms