Slide 1 Vitaly Shmatikov CS 345 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.
Dynamic Memory Management
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
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?
Heap Management.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
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
CS 536 Spring Automatic Memory Management Lecture 24.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
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.
G Robert Grimm New York University Cool Pet Tricks with… …Virtual Memory.
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
© Richard Jones, Eric Jul, mmnet GC & MM Summer School, July A Rapid Introduction to Garbage Collection Richard Jones Computing Laboratory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
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.
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.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
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.
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.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
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.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
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.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
Dynamic Compilation Vijay Janapa Reddi
Concepts of programming languages
Simulated Pointers.
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Strategies for automatic memory management
Chapter 12 Memory Management
Heap storage & Garbage collection
Heap storage & Garbage collection
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:

slide 1 Vitaly Shmatikov CS 345 Garbage Collection

slide 2 Major Areas of Memory uStatic area Fixed size, fixed content, allocated at compile time uRun-time stack Variable size, variable content (activation records) Used for managing function calls and returns uHeap Fixed size, variable content Dynamically allocated objects and data structures –Examples: ML reference cells, malloc in C, new in Java

slide 3 Cells and Liveness uCell = data item in the heap Cells are “pointed to” by pointers held in registers, stack, global/static memory, or in other heap cells uRoots: registers, stack locations, global/static variables uA cell is live if its address is held in a root or held by another live cell in the heap

slide 4 Garbage uGarbage is a block of heap memory that cannot be accessed by the program An allocated block of heap memory does not have a reference to it (cell is no longer “live”) Another kind of memory error: a reference exists to a block of memory that is no longer allocated uGarbage collection (GC) - automatic management of dynamically allocated storage Reclaim unused heap blocks for later use by program

slide 5 Example of Garbage class node { int value; node next; } node p, q; p = new node(); q = new node(); q = p; delete p;

slide 6 Why Garbage Collection? uToday’s programs consume storage freely 1GB laptops, 1-4GB deskops, 8-512GB servers 64-bit address spaces (SPARC, Itanium, Opteron) u… and mismanage it Memory leaks, dangling references, double free, misaligned addresses, null pointer dereference, heap fragmentation Poor use of reference locality, resulting in high cache miss rates and/or excessive demand paging uExplicit memory management breaks high-level programming abstraction

slide 7 GC and Programming Languages uGC is not a language feature uGC is a pragmatic concern for automatic and efficient heap management Cooperative langs: Lisp, Scheme, Prolog, Smalltalk … Uncooperative languages: C and C++ –But garbage collection libraries have been built for C/C++ uRecent GC revival Object-oriented languages: Modula-3, Java –In Java, runs as a low-priority thread; System.gc may be called by the program Functional languages: ML and Haskell

slide 8 The Perfect Garbage Collector uNo visible impact on program execution uWorks with any program and its data structures For example, handles cyclic data structures uCollects garbage (and only garbage) cells quickly Incremental; can meet real-time constraints uHas excellent spatial locality of reference No excessive paging, no negative cache effects uManages the heap efficiently Always satisfies an allocation request and does not fragment

slide 9 Summary of GC Techniques uReference counting Directly keeps track of live cells GC takes place whenever heap block is allocated Doesn’t detect all garbage uTracing GC takes place and identifies live cells when a request for memory fails Mark-sweep Copy collection uModern techniques: generational GC

slide 10 Reference Counting uSimply count the number of references to a cell uRequires space and time overhead to store the count and increment (decrement) each time a reference is added (removed) Reference counts are maintained in real-time, so no “stop-and-gag” effect Incremental garbage collection uUnix file system uses a reference count for files uC++ “smart pointer” (e.g., auto_ptr) use reference counts

slide 11 Reference Counting: Example 1 root set Heap space

slide 12 Reference Counting: Strengths uIncremental overhead Cell management interleaved with program execution Good for interactive or real-time computation uRelatively easy to implement uCan coexist with manual memory management uSpatial locality of reference is good Access pattern to virtual memory pages no worse than the program, so no excessive paging uCan re-use freed cells immediately If RC == 0, put back onto the free list

slide 13 Reference Counting: Weaknesses uSpace overhead 1 word for the count, 1 for an indirect pointer uTime overhead Updating a pointer to point to a new cell requires: –Check to ensure that it is not a self-reference –Decrement the count on the old cell, possibly deleting it –Update the pointer with the address of the new cell –Increment the count on the new cell uOne missed increment/decrement results in a dangling pointer / memory leak uCyclic data structures may cause leaks

slide 14 Reference Counting: Cycles 1 root set Heap space Memory leak

slide 15 T* obj: int cnt: 2 object of type T RefObj *ref RefObj Ref RefObj *ref x y sizeof(RefObj ) = 8 bytes of overhead per reference-counted object sizeof(Ref ) = 4 bytes Fits in a register Easily passed by value as an argument or result of a function Takes no more space than regular pointer, but much “safer” (why?) “Smart Pointer” in C++ uSimilar to std::auto_ptr in ANSI C++

slide 16 Smart Pointer Implementation template class RefObj { T* obj; int cnt; public: RefObj(T* t) : obj(t), cnt(0) {} ~RefObj() { delete obj; } int inc() { return ++cnt; } int dec() { return --cnt; } operator T*() { return obj; } operator T&() { return *obj; } T& operator *() { return *obj; } }; template class Ref { RefObj * ref; Ref * operator&() {} public: Ref() : ref(0) {} Ref(T* p) : ref(new RefObj (p)) { ref->inc();} Ref(const Ref & r) : ref(r.ref) { ref->inc(); } ~Ref() { if (ref->dec() == 0) delete ref; } Ref & operator=(const Ref & that) { if (this != &that) { if (ref->dec() == 0) delete ref; ref = that.ref; ref->inc(); } return *this; } T* operator->() { return *ref; } T& operator*() { return *ref; } };

slide 17 Using Smart Pointers Ref proc() { Ref s = new string(“Hello, world”); // ref count set to 1 … int x = s->length(); // s.operator->() returns string object ptr … return s; } // ref count goes to 2 on copy out, then 1 when s is auto-destructed int main() { … Ref a = proc(); // ref count is 1 again … } // ref count goes to zero and string is destructed, along with Ref and RefObj objects

slide 18 Mark-Sweep Garbage Collection uEach cell has a mark bit uGarbage remains unreachable and undetected until heap is used up; then GC goes to work, while program execution is suspended uMarking phase Starting from the roots, set the mark bit on all live cells uSweep phase Return all unmarked cells to the free list Reset the mark bit on all marked cells

slide 19 root set Heap space Mark-Sweep Example (1)

slide 20 root set Heap space Mark-Sweep Example (2)

slide 21 root set Heap space Mark-Sweep Example (3)

slide 22 root set Heap space Mark-Sweep Example (4) Reset mark bit of marked cells Free unmarked cells

slide 23 Mark-Sweep Costs and Benefits uGood: handles cycles correctly uGood: no space overhead 1 bit used for marking cells may limit max values that can be stored in a cell (e.g., for integer cells) uBad: normal execution must be suspended uBad: may touch all virtual memory pages May lead to excessive paging if the working-set size is small and the heap is not all in physical memory uBad: heap may fragment Cache misses, page thrashing; more complex allocation

slide 24 Copying Collector uDivide the heap into “from-space” and “to-space” uCells in from-space are traced and live cells are copied (“scavenged”) into to-space To keep data structures linked, must update pointers for roots and cells that point into from-space –This is why references in Java and other languages are not pointers, but indirect abstractions for pointers Only garbage is left in from-space uWhen to-space fills up, the roles flip Old to-space becomes from-space, and vice versa

slide 25 Copying a Linked List from-space to-space root A C B D forwarding address pointer [Cheney’s algorithm] A’B’C’D’ Cells in to-space are packed

slide 26 Flipping Spaces to-space from-space forwarding address pointer A’B’C’D’ root

slide 27 Copying Collector Tradeoffs uGood: very low cell allocation overhead Out-of-space check requires just an addr comparison Can efficiently allocate variable-sized cells uGood: compacting Eliminates fragmentation, good locality of reference uBad: twice the memory footprint Probably Ok for 64-bit architectures (except for paging) –When copying, pages of both spaces need to be swapped in. For programs with large memory footprints, this could lead to lots of page faults for very little garbage collected –Large physical memory helps

slide 28 Generational Garbage Collection uObservation: most cells that die, die young Nested scopes are entered and exited more frequently, so temporary objects in a nested scope are born and die close together in time Inner expressions in Scheme are younger than outer expressions, so they become garbage sooner uDivide the heap into generations, and GC the younger cells more frequently Don’t have to trace all cells during a GC cycle Periodically reap the “older generations” Amortize the cost across generations

slide 29 Generational Observations uCan measure “youth” by time or by growth rate uCommon Lisp: 50-90% of objects die before they are 10KB old uGlasgow Haskell: 75-95% die within 10KB No more than 5% survive beyond 1MB uStandard ML of NJ reclaims over 98% of objects of any given generation during a collection uC: one study showed that over 1/2 of the heap was garbage within 10KB and less than 10% lived for longer than 32KB

slide 30 Young Old root set A B C D E F G Example with Immediate “Aging” (1)

slide 31 Young Old root set A B D E F G C Example with Immediate “Aging” (2)

slide 32 Youngest Oldest To-space From-space To-space root set Middle generation(s) Generations with Semi-Spaces