Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Similar documents
Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Lecture 13: Garbage Collection

CMSC 330: Organization of Programming Languages

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection

Garbage Collection. Hwansoo Han

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CMSC 330: Organization of Programming Languages

Myths and Realities: The Performance Impact of Garbage Collection

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Lecture 15 Garbage Collection

Run-Time Environments/Garbage Collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

Programming Language Implementation

CS61C : Machine Structures

Exploiting the Behavior of Generational Garbage Collector

Implementation Garbage Collection

Garbage Collection Algorithms. Ganesh Bikshandi

G Programming Languages - Fall 2012

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Run-time Environments -Part 3

Garbage Collection (1)

Habanero Extreme Scale Software Research Project

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Heap Management. Heap Allocation

Automatic Garbage Collection

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Automatic Memory Management

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

CS 241 Honors Memory

CS61C : Machine Structures

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

Name, Scope, and Binding. Outline [1]

Opera&ng Systems CMPSCI 377 Garbage Collec&on. Emery Berger and Mark Corner University of Massachuse9s Amherst

A new Mono GC. Paolo Molaro October 25, 2006

High-Level Language VMs

Lecture Notes on Garbage Collection

Garbage Collection Techniques

Garbage Collection. Steven R. Bagley

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Ulterior Reference Counting: Fast Garbage Collection without a Long Wait

Review. Partitioning: Divide heap, use different strategies per heap Generational GC: Partition by age Most objects die young

Compiler Construction D7011E

CS61C : Machine Structures

Structure of Programming Languages Lecture 10

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Lecture 13: Complex Types and Garbage Collection

Robust Memory Management Schemes

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

CS61C : Machine Structures

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

IBM Research Report. Efficient Memory Management for Long-Lived Objects

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Reference Counting. Reference counting: a way to know whether a record has other users

Lecture 15 Advanced Garbage Collection

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Garbage Collection Basics

CSCI-1200 Data Structures Spring 2018 Lecture 25 Garbage Collection & Smart Pointers

Dynamic Memory Management

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Older-First Garbage Collection in Practice: Evaluation in a Java Virtual Machine

CA341 - Comparative Programming Languages

Dynamic Memory Management

Cycle Tracing. Presented by: Siddharth Tiwary

CS842: Automatic Memory Management and Garbage Collection. Mark and sweep

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Garbage Collection (2) Advanced Operating Systems Lecture 9

Short-term Memory for Self-collecting Mutators. Martin Aigner, Andreas Haas, Christoph Kirsch, Ana Sokolova Universität Salzburg

Dynamic Memory Allocation

Dynamic Memory Management! Goals of this Lecture!

Heap Compression for Memory-Constrained Java

Advanced Programming & C++ Language

The C4 Collector. Or: the Application memory wall will remain until compaction is solved. Gil Tene Balaji Iyengar Michael Wolf

CS2210: Compiler Construction. Runtime Environment

CS61C : Machine Structures

Compiler Construction

Dynamic Storage Allocation

Lecture Notes on Garbage Collection

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

Garbage Collection. Vyacheslav Egorov

Hard Real-Time Garbage Collection in Java Virtual Machines

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Shenandoah An ultra-low pause time Garbage Collector for OpenJDK. Christine H. Flood Roman Kennke

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Lecture Notes on Advanced Garbage Collection

Myths and Realities: The Performance Impact of Garbage Collection

Concurrent Garbage Collection

Dynamic Memory Allocation

Transcription:

Garbage Collection Last time Compiling Object-Oriented Languages Today Motivation behind garbage collection Garbage collection basics Garbage collection performance Specific example of using GC in C++ Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides CS553 Lecture Garbage Collection 1 Background Static allocation: variables are bound to storage at compile-time pros: easy to implement cons: no recursion, data structure sizes are compile-time constants, data structures cannot be dynamic Stack allocation: dyn. alloc. stack frame for each proc. invocation pros: recursion is possible, data structure sizes may depend on parameters cons: stack allocated data is not persistent, stack allocated data cannot outlive the procedure for which it is defined Heap allocation: arbitrary alloc. and dealloc. of objects in *heap* pros: solves above problems: dynamic, persistent data structures cons: very difficult to explicitly manage heap CS553 Lecture Garbage Collection 2 1

Memory Management Ideal deallocate all data that will not be used in the future (not possible) What is garbage? Manual/Explicit programmer deallocates with free or delete Automatic/Implicit garbage collection CS553 Lecture Garbage Collection 3 Explicit versus Automatic Explicit + efficiency can be very high + gives programmers control more code to maintain correctness is difficult core dump if an object is freed too soon space is wasted if an object is freed too late if never free, at best waste space, at worst fail Automatic + reduces programmer burden + eliminates sources of errors + integral to modern OOP languages (ie. Java, C#) can not determine all objects that won t be used in the future may or may not hurt performance CS553 Lecture Garbage Collection 4 2

Key Issues For both Fast allocation Fast reclamation Low fragmentation (wasted space) For Garbage Collection How to discriminate between live objects and garbage Basic approaches to garbage collection reference counting reachability CS553 Lecture Garbage Collection 5 Reference Counting Idea for each heap allocated object, maintain count of # of pointers to it when creating object x, rc[x] = 0 when creating new reference to object x, rc[x]++ when removing reference to object x, rc[x]-- if ref count goes to zero, free object (i.e., place on free list) Example Node x, y; x = new Node (3, null); y = x; x = null; y = x; Complication what if freed object contains pointers? y x null Node rc = 1 2 1 0 CS553 Lecture Garbage Collection 6 3

Reference Counting Analysis How it handles key issues allocation is expensive because searching a freelist reclamation is local and incremental fragmentation is high Further analysis + relatively simple + very simple run-time system cannot reclaim cyclic data structures (shifts burden to programmer) high runtime overhead (must manipulate ref counts for every reference update) space cost complicates compilation CS553 Lecture Garbage Collection 7 Trace Collecting Observation rather than explicitly keep track of the number of references to each object we can traverse all reachable objects and discard unreachable objects Details start with a set of root pointers (program vars) global pointers pointers in stack and registers traverse objects recursively from root set visit reachable objects unvisited objects are garbage we might visit an object even if it's dynamically dead (ie, we are only conservatively approximating dead object discovery) When do we collect? when the heap is full CS553 Lecture Garbage Collection 8 4

Mark-Sweep Collecting Simple trace collector trace reachable objects marking reachable objects sweep through all of heap add unmarked objects to free list clear marks of marked objects Example x T T T y F T F CS553 Lecture Garbage Collection 9 Mark-Sweep Collecting Analysis How it handles the key issues allocation is expensive because searching a freelist reclamation can result in the embarrassing pause problem poor memory locality when tracing fragmentation is high Further analysis collects cyclic structures simple must be able to dynamically identify pointers in vars and objects more complex runtime system space overhead is only one bit per data object CS553 Lecture Garbage Collection 10 5

Mark-Compact Collecting or Copy Collecting Idea move objects to new heap while tracing Details divide heap in half (prog. allocs. in from-space, to-space is empty) when from-space is full... copy non-garbage from from-space to to-space (to-space is compact) when visiting object during tracing copy from from-space to to-space leave forwarding pointer in from-space version of object if revisit this object, redirect pointer to to-space copy CS553 Lecture Garbage Collection 11 Copying Garbage Collection from to space from to space CS553 Lecture Garbage Collection 12 6

Mark-Compact Collecting Analysis How it handles the key issues allocation is very fast since there is no free list to search reclamation can result in the embarrassing pause problem poor memory locality when tracing copying data from one heap to another changing pointers because objects are being moved no fragmentation Further Analysis + collects cyclic structures requires twice the (virtual) memory breadth-first traversal means to-space objects could have poor locality CS553 Lecture Garbage Collection 13 Hybrid Collectors Idea different collection techniques may be combined Example: Mark-Sweep/Copy collector big objects managed with mark-sweep (avoids copy time) small objects managed with copy collector Analysis + may be more efficient more complex CS553 Lecture Garbage Collection 14 7

Generational Collecting Observation "young" objects are most likely to die soon, while "old objects are more likely to live on Idea exploit this fact by concentrating collection on "young" objects Details divide heap in generations (G0, G1,...; G0 for youngest objects) collect G0 most frequently, G1 less frequently, etc. object is tenured from one gen. to next after surviving several GCs Result usually only have to collect a small sub-heap CS553 Lecture Garbage Collection 15 Generational Collecting (cont) Additional issues need to encode age in object root set for objects in one generation may come from another gen. generation Gi should be k times larger than Gi-1 each generation may be collected with different algorithm Dealing with cross-generation pointers older to younger (i.e., Gi to Gj for i>j) are uncommon search all of Gi? remembered lists (compiler support) write barriers younger to older (i.e., Gj to Gi for i>j) are very common collect Gj when collecting Gi CS553 Lecture Garbage Collection 16 8

Generational Collecting Analysis How it handles the key issues allocation in the youngest heap is fast if a copy collector is used reclamation is fast because doing collection on smaller heap fragmentation depends on collector used in each heap Further Analysis + less memory is required if use mark-sweep for older generations + possibly better locality still sometimes do full, slow collections (embarrassing pause!) need to record age with each object CS553 Lecture Garbage Collection 17 Who does what? Pointers Issues in order to trace reachable objects, we must be able to dynamically determine what is a pointer imagine doing this in C! easier in Java how? compiler support and/or runtime tagging convention about what can be a pointer what if we re not certain about what is a pointer? be conservative; assume anything that may be a pointer is may keep extra garbage can not move objects (mark-compact) conservative garbage collectors can be used with C CS553 Lecture Garbage Collection 18 9

Who does what? Scheduling Garbage Collection Generally allocation is no longer possible, garbage collection is necessary VM usually stops all mutator threads JIT generated code must properly handle out-of-memory exception Write barriers (for reference counting and generational collection) Each time a write to a pointer occurs, a write barrier catches this and performs some action generation collection needs the write barrier to keep track of pointers from older generations to new generations reference counting requires write barriers to detect when any pointer changes CS553 Lecture Garbage Collection 19...Performance Impact of Garbage Collection [Blackburn et al 2004] Experiment setup use Jikes RVM (research virtual machine), highly optimized MMtk (The Memory Management Toolkit) is a framework for construction of garbage collectors within Jikes RVM. Machines: Athlon (best performance), Pentium 4, Power PC Benchmarks: SPEC JVM benchmarks and pseudojbb (variant of SPEC JBB2000) garbage collection algorithms semi-space, a copying tracing collector mark-sweep reference-counting CS553 Lecture Garbage Collection 20 10

[Blackburn et al 2004] Some Conclusions Contiguous allocation is better than free-list allocators mutator performance is 5-15% better due to improved data locality Generational collectors are better than whole heap collectors write barrier overhead is only (2-14%) and is outweighed by improvements in collection time Tracing collection is better than reference counting the overhead of reference counting is too expensive reference counting can be beneficial for older generations of the heap Nursery size in generational collector should be about 4-8MB debunks the myth that the size should be about L2 cache size (512KB) have to get to the point where constant number of roots (about 64KB) CS553 Lecture Garbage Collection 21 Concrete Memory Management Problem OpenAnalysis goal is to do program analysis of large programs, therefore can t just leak memory explicit management is very error prone difficult to debug segfaults for analysis results it isn t clear which objects should own which other objects, therefore an explicit management policy proved very problematic Options use one of the conservative garbage collectors have portability issues smart pointers CS553 Lecture Garbage Collection 22 11

Smart Pointers auto_ptr (in C++ standard) basic idea void f() { auto_ptr<int> p = new int; *p = 5;... // the dynamically created object is deleted when // p goes out of scope } problem is that only one auto_ptr can point to a particular object at any one time CS553 Lecture Garbage Collection 23 OA_ptr in OpenAnalysis Goals allow multiple smart ptrs to own the same object, similar to shared_ptr in Boost library catch as many common errors statically as possible allow the smart pointer to be used just like a normal pointer as much as possible use simple template mechanisms so as not to confuse many C++ compilers Details implemented using reference counting the OA_ptr class has a pointer to the object and a pointer to a reference counter CS553 Lecture Garbage Collection 24 12

Some tricky stuff Allowing polymorphism OA_ptr<foo> p; p = new bar; // bar is a subclass of foo p->hello(); // hello is a virtual method (*p).hello(); Override the access operators T* operator->() const { assert(mptr!= NULL); return mptr; } T& operator*() const { assert(mptr!= NULL); return *mptr; } CS553 Lecture Garbage Collection 25 Allow type conversions Want to pass in a subclass to a base class int foo( OA_ptr<base> ) {...} int main() { OA_ptr<sub> p; p = new sub; foo(sub); } Want to pass in a subclass to a base class template <class T> class OA_ptr {... // so that can pass a subclass into base class // Stroustrup 349-350 template <class T2> operator OA_ptr<T2> () const { return OA_ptr<T2>(mPtr,mRefCountPtr); } CS553 Lecture Garbage Collection 26 13

Not a perfect solution Decided against raw pointer comparison to catch more static errors OA_ptr<Location::Location> loc = r->foo(mre); if (!loc.ptrequal(null)) {... } Returning an OA_ptr OA_ptr<LocIterator> AliasMap::getMayLocs(MemRefHandle ref) { int setid = mmemreftoidmap[ref]; OA_ptr<AliasMapLocIter> retval; retval = new AliasMapLocIter(mIdToLocSetMap[setId]); return retval; } Usage that causes dynamic errors OA_ptr<Location> loc; NamedLoc myloc; // DO NOT assign the "this" pointer to an OA_ptr!!! loc = this; // DO NOT assign the address of a local to an OA_ptr!!! loc = &myloc; CS553 Lecture Garbage Collection 27 Summary Categorizing garbage collection algorithms how is garbage identified? reference counting or tracing when is it collected? incrementally, generationally, or all at once what allocator is used? contiguous/bump allocator or freelist what mechanism is used for reclamation? copying or put on freelist how is the heap space managed? split for copying or generations? Open problems in garbage collection still no conclusive evidence that it is always faster or slower than explicit memory management how can we measure whether it is or not? CS553 Lecture Garbage Collection 28 14

Next Time Friday Dr. Sanjay Rajapadhye there will be questions from his lecture on the final, come and take notes Assignments turn in HW3 soon CS553 Lecture Garbage Collection 29 15