CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.

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.
Garbage collection (& Midterm Topics) David Walker COS 320.
1 Overview Assignment 5: hints  Garbage collection Assignment 4: solution.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
ICS220 – Data Structures and Algorithms Lecture 13 Dr. Ken Cosh.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
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.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.
Bounding Space Usage of Conservative Garbage Collectors Ohad Shacham December 2002 Based on work by Hans-J. Boehm.
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.
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
Ceg860 (Prasad)L9GC1 Memory Management Garbage Collection.
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
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.
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.
© Richard Jones, Eric Jul, mmnet GC & MM Summer School, July A Rapid Introduction to Garbage Collection Richard Jones Computing Laboratory.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
Linked lists and memory allocation Prof. Noah Snavely CS1114
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.
A Parallel, Real-Time Garbage Collector Author: Perry Cheng, Guy E. Blelloch Presenter: Jun Tao.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management.
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,
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
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.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
Garbage Collection What is garbage and how can we deal with it?
Storage 18-May-18.
Dynamic Compilation Vijay Janapa Reddi
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
Automatic Memory Management
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Data Structures and Algorithms
Data Structures and Algorithms
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Automating Memory Management
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 2 Administration Graded Prelim 2 is back Programming Assignment 4 due in one week Reading: Appel 13

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 3 Outline Overview of various garbage collection techniques and impact on compiled code: –Mark and sweep garbage collection –Reference counting GC –Copying GC –Generational GC More topics in Appel: –concurrent/incremental garbage collection –heap management

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 4 Garbage collection Garbage collection: the process of reclaiming memory unused by the program Usually most complex part of the run- time environment Several different techniques, with different implications for code generation

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 5 Problem Java, Iota +, C++ have new operator that allocates new memory How do we get it back when the object is not needed any longer? C++: explicit memory management – delete operator destroys object, allows reuse of its memory -- programmer decides how to collect garbage –makes modular programming difficult—have to know what code “owns” every object so that objects are deleted exactly once

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 6 Automatic garbage collection Want to delete objects automatically when they won’t be used again Conservative approach: delete only objects that definitely won’t be used again Reachability: objects definitely won’t be used again if there is no way to reach them from root references that are always accessible

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 7 Object graph Stack, registers are treated as the roots of the object graph. Anything not reachable from roots is garbage How can non-reachable objects can be reclaimed efficiently? Compiler can help

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 8 Mark and sweep collection Classic algorithm with two phases Phase 1: Mark all reachable objects –start from roots and traverse graph forward marking every object reached Phase 2: Sweep up the garbage –Walk over all allocated objects and check for marks –Unmarked objects are reclaimed –Marked objects have their marks cleared

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 9 Traversing the object graph

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 10 Implementing mark phase Mark and sweep generally implemented as depth-first traversal of object graph Has natural recursive implementation What happens when we try to mark a long linked list recursively?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 11 Pointer reversal Idea: during DFS, each pointer only followed once. Can reverse pointers after following them -- no recursion needed! (Deutsch-Waite-Schorr alg.) Implication: objects are broken while being traversed; all computation over objects must be halted during mark phase (oops)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 12 Conservative Mark & Sweep Allocated storage contains both pointers and non-pointers; integers may look like pointers Treating a pointer as a non-pointer: objects may be garbage-collected even though they are still reachable and in use Treating a non-pointer as a pointer: objects are not garbage collected even though they are not pointed to (safe) Conservative collection: assumes things are pointers unless they can’t be; requires no language support (works for C!)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 13 Cost of mark and sweep Mark and sweep algorithm reads all memory in use by program: run time is proportional to total amount of data (live or garbage) Can pause program for long periods! Basic mark & sweep requires ability to manage heap of variable-sized objects; typical heap implementation only allocates memory in 2 n byte units to avoid fragmentation, make allocation/deallocation fast. 30% hit

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 14 Reference counting Old algorithm for automatic garbage collection: associate with every object a reference count that is the number of incoming pointers When number of incoming pointers is zero, object is unreachable: garbage Compiler emits extra code to increment and decrement reference counts automatically

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 15 Reference counts Reference counting doesn’t detect cycles!

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 16 Other problems Extra code to increment and decrement reference counts has performance penalty: 5-30% depending on sophistication. Consider assignment x.b = y Without ref-counts: mov [tx + b_off], ty With ref-counts: t1 = M[tx + b_off]; c = M[t1 + refcnt]; c = c - 1; M[t1 + refcnt] = c; if (c == 0) goto L1 else goto L2; L1: call free(t1); L2: M[tx + b_off] = ty; c = M[ty + refcnt]; c = c + 1; M[ty + refcnt] = c; Data-flow analysis can be used to avoid unnecessary increments & decrements End result: reference counting not used much by real language implementations

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 17 Copying collection Like mark & sweep: collects all garbage Basic idea: keep two memory heaps around. One heap in use by program; other sits idle until GC requires it GC copies all live objects from active heap to the other; dead objects discarded en masse. Heaps then switch roles. During collection, heaps are called from-space and to-space

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 18 Copying collection (Cheney’s) Copying starts by moving all root objects from from-space to to-space From space traversed breadth-first from roots, objects encountered are copied to top of to-space. scan next from space to-space

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 19 Benefits of copying collection Once scan=next, all uncopied objects are garbage. Root pointers (registers, stack) are swung to point into to-space, making it active Copying collection has nice properties: –Simple, can be done concurrently with execution –Run time proportional to # live objects –Automatically eliminates fragmentation by compacting memory –malloc(n) implemented as return (top += n); Precise pointer/non-pointer information required

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 20 Generational GC Observation: if an object has been reachable for a long time, it is likely to remain so In long-running system, mark & sweep, copying collection waste time scanning and/or copying these older objects Approach: assign objects to different generations G 0, G 1, G 2,… Generation G 0 contains newest objects, most likely to become garbage (<10% live)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 21 Generations Consider a two-generation system. G 0 = new objects, G 1 = tenured objects New generation is scanned for garbage much more often than tenured objects New objects eventually given tenure if they last long enough Roots of garbage collection for collecting G 0 include all objects in G 1 (as well as stack, registers)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 22 Remembered set Since tenured objects are roots, do we have to scan tenured objects after all? In practice, few tenured objects will point to new objects; unusual for an object to point to a newer object Can only happen if older object is modified long after creation to point to new object Compiler inserts extra code on object field writes to catch modifications to older objects—older objects are remembered set for scanning during GC, tiny fraction of G 1

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 23 Summary Like linking, garbage collection is an aspect of the program environment with implications for compilation Conservative generational garbage collectors are available; Boehm/Demers/Weiser collector could be used with Iota, Iota +. In fact, there is little reason not to. Concurrent garbage collectors also exist: GC in background GC is here to stay! (Thanks to Java)