Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Garbage Collection Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc01.html.

Slides:



Advertisements
Similar presentations
An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.
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.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
By Jacob SeligmannSteffen Grarup Presented By Leon Gendler Incremental Mature Garbage Collection Using the Train Algorithm.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
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.
1 The Compressor: Concurrent, Incremental and Parallel Compaction. Haim Kermany and Erez Petrank Technion – Israel Institute of Technology.
Garbage Collection Mooly Sagiv html://
Memory Management Professor Yihjia Tsai Tamkang University.
MOSTLY PARALLEL GARBAGE COLLECTION Authors : Hans J. Boehm Alan J. Demers Scott Shenker XEROX PARC Presented by:REVITAL SHABTAI.
Memory Management Chapter 5 Mooly Sagiv
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
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
Incremental Garbage Collection
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
Uniprocessor Garbage Collection Techniques Paul R. Wilson.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Garbage Collection Memory Management Garbage Collection –Language requirement –VM service –Performance issue in time and space.
8/14/2015© Hal Perkins & UW CSEW-1 CSE P 501 – Compilers Memory Management & Garbage Collection Hal Perkins Winter 2008.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Chapter 7 Run-Time Environments
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
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.
Reference Counting. Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for.
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.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Garbage Collection What is garbage and how can we deal with it?
Names and Attributes Names are a key programming language feature
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Rifat Shahriyar Stephen M. Blackburn Australian National University
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Optimizing Malloc and Free
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Closure Representations in Higher-Order Programming Languages
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
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Reference Counting vs. Tracing
Presentation transcript:

Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Garbage Collection Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc01.html

Garbage Collection ROOT SET HEAP a b c d e f

What is garbage collection The runtime environment reuse records that were allocated but are not subsequently used garbage records not live It is undecidable to find the garbage records: Decidability of liveness Decidability of type information conservative collection every live record is identified some garbage run-time records are not identified Find the reachable records via pointer chains Often done in the allocation function

stack heap let type list = {link: list, key: int} type tree = {key: int, left: tree, right: tree} let var x := list {link=nil, key=7} var y := list {link=x, key=9} in x.link := y end; in let var p := maketree()` var r := p.right var q := r.key in showtree(r) end end 7 link link 9 x y

stack heap let type list = {link: list, key: int} type tree = {key: int, left: tree, right: tree} let var x := list {link=nil, key=7} var y := list {link=x, key=9} in x.link := y end; in let var p := maketree()` var r := p.right var q := r.key in showtree(r) end end 7 link key key link 9 x y

let type list = {link: list, key: int} right 12 left 15 20 37 59 let type list = {link: list, key: int} type tree = {key: int, left: tree, right: tree} let var x := list {link=nil, key=7} var y := list {link=x, key=9} in x.link := y end; in let var p := maketree()` var r := p.right var q := r.key in showtree(r) end end p q 37 r link 7 link 9

Outline Why is it needed? Why is it taught? Mark-and-Sweep Collection Reference Counts Copying Collection Generational Collection Incremental Collection Interfaces to the Compiler

Garbage Collection vs. Explicit Memory Deallocation Faster program development Less error prone Can lead to faster programs Support very general programming styles, e.g. higher order programming Standard in ML and Java Supported in C and C++ via separate libraries Can improve locality of references May require more space Needs a large memory Can lead to long pauses Can change locality of references Effectiveness depends on programming language and style Hides documentation More trusted code

A Pathological C Program a = malloc(…) ; b = a; free (a); c = malloc (…); if (b == c) printf(“unexpected equality”);

Interesting Aspects of Garbage Collection Data structures Non constant time costs Amortized algorithms Constant factors matter Interfaces between compilers and run-time environments Interfaces between compilers and virtual memory management

Mark-and-Sweep Collection Mark the records reachable from the roots (stack and static variables and machine registers) Sweep the heap space by moving unreachable records to the freelist

The Mark Phase for each root v DFS(v) function DFS(x) if x is a pointer and record x is not marked mark x for each field fi of record x DFS(x.fi)

The Sweep Phase p := first address in heap while p < last address in the heap if record p is marked unmark p else let f1 be the first pointer field in p p.f1 := freelist freelist := p p := p + size of record p

right 12 left 15 20 37 59 Mark p q 37 r link 7 link 9

right 12 left 15 20 37 59 Sweep p q 37 r link 7 freelist link 9

right 12 left 15 20 37 59 p q 37 r link 7 freelist link 9

Cost of GC The cost of a single garbage collection can be linear in the size of the store may cause quadratic program slowdown Amortized cost collection-time/storage reclaimed Cost of one garbage collection c1 R + c2 H H - R Reclaimed records Cost per reclaimed record (c1 R + c2 H)/ (H - R) If R/H > 0.5 increase H if R/H < 0.5 cost per reclaimed word is c1 + 2c2 ~16 There is no lower bound

Efficient implementation of DFS Explicit stack Pointer reversal Other data structures

Freelist may be implemented as an array of lists Fragmentation External Too many small records Internal A use of too big record without splitting the record Freelist may be implemented as an array of lists

Maintain a counter per record Reference Counts Maintain a counter per record The compiler generates code to update counter Constant overhead per instruction Cannot reclaim cyclic elements Many instructions for destructive updates z := x.fi ;c := z.count z.count := c if (--c = 0) goto putonFreeList x.fi := p ; p.count++; x.fi := p

right 12 left 15 20 37 59 1 p q 37 1 r link 1 7 2 1 1 1 link 9

Copying Collection Maintains two separate heaps from-space and to-space pointer next to the next free record in from-space A pointer limit to the last record in from-space If next = limit copy the reachable records from from-space into to-space set next and limit Switch from-space and to-space Requires type information

Breadth-first Copying Garbage Collection next := beginning of to-space scan := next for each root r r := Forward(r) while scan < next for each field fi of record at scan scan.fi := Forward(scan.fi) scan := scan + size of record at scan

The Forwarding Procedure function Forward(p) if p points to from-space then if p.f1 points to to-space return p.f1 else for each field fi of p next.fi := p.fi p.f1 := next next := next size of record p else return p

right 12 left 15 20 37 59 p q 37 r link 7 link 9

right 12 left 20 37 59 scan 15 left p right next q 37 r link 7 link 9

12 scan p q 37 r next 1 15 left left right 37 left right right link 7 20 59 scan 15 left p right q 37 37 r left right next link 7 1 link 9

scan p q 37 r next left 15 left left right 37 left right right link 12 59 left right next 20 left link right 9

12 p q 37 scan r next left 15 left left right 37 left right right link 59 left 20 right left link right 9

Amortized Cost of Copy Collection c3R / (H/2 - R)

Locality of references Copy collection does not create fragmentation Cheney's algorithm may lead to subfields that point to far away records poor virtual memory and cache performance DFS normally yields better locality but is harder to implement DFS may also be bad for locality for records with more than one pointer fields A compromise is a hybrid breadth first search with two levels down (Semi-depth first forwarding)

The New Forwarding Procedure function Chase(p) repeat q := next next := next +size of record p r := nil for each field fi of p q.fi := p.fi if q.fi points to from-space and q.fi.f1 does not point to to-space then r := q.fi p.f1 := q p := r until p = nil function Forward(p) if p points to from-space then if p.f1 points to to-space return p.f1 else Chase(p); return p.f1 else return p

Generational Garbage Collection Newly created objects contain higher percentage of garbage Partition the heap into generations G1 and G2 First garbage collect the G1 heap records which are reachable After two or three collections are promoted to G2 Once a while garbage collect G2 Can be generalized to more than two heaps But how can we garbage collect in G1?

Scanning roots from older generations remembered list The compiler generates code after each destructive update b.fi := a to put b into a vector of updated objects scanned by the garbage collector remembered set remembered-list + “set-bit”' Card marking Divide the memory into 2k cards Page marking k = page size virtual memory system catches updates to old-generations using the dirty-bit

Incremental Collection Even the most efficient garbage collection can interrupt the program for quite a while Under certain conditions the collector can run concurrently with the program (mutator) Need to guarantee that mutator leaves the records in consistent state, e.g., may need to restart collection Two solutions compile-time Generate extra instructions at store/load virtual-memory Mark certain pages as read(write)-only a write into (read from) this page by the program restart mutator

Tricolor marking Generalized GC Three kinds of records White Grey Not visited (not marked or not copied) Grey Marked or copied but children have not been examined Black Marked and their children are marked

Basic Tricolor marking while there are any grey objects select a grey record p for each field fi of record p if record p.fi is white color record p.fi grey color record p black Invariants No black points to white Every grey is on the collector's (stack or queue) data structure

Establishing the invariants Dijkstra, Lamport, et al Mutator stores a white pointer a into a black pointer b color a grey (compile-time) Steele color b grey (compile-time) Boehm, Demers, Shenker All black pages are marked read-only A store into black page mark all the objects in this page grey (virtual memory system) Baker Whenever the mutator fetches a pointer b to a grey or white object Appel, Ellis, Li Whenever the mutator fetches a pointer b from a page containing a non black object color every object on this page black and children grey (virtual memory system)

Interfaces to the Compiler The semantic analysis identifies record fields which are pointers and their size Generate runtime descriptors at the beginning of the records Pass the descriptors to the allocation function The compiler also passes pointer-map the set of live pointer locals, temporaries, and registers Recorded at ?-time for every procedure Pointer-map can be keyed by return-address

Allocation of a Records Call the allocate function Test next + N < limit and maybe call garbage collector Move next into result Clear M[next], M[next+1],…, M[next+N-1] next := next + N Return from the allocate function Move result into some computationally useful place Store useful values into the record

Summary Garbage collection is an effective technique Leads to more secure programs Tolerable cost But is not used in certain applications Realtime May be improved