Memory Management Kathryn McKinley.

Slides:



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

Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Automatic Memory Management Noam Rinetzky Schreiber 123A /seminar/seminar1415a.html.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
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.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
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.
MC 2 : High Performance GC for Memory-Constrained Environments N. Sachindran, E. Moss, E. Berger Ivan JibajaCS 395T *Some of the graphs are from presentation.
380C Where are we & where we are going – Managed languages Dynamic compilation Inlining Garbage collection What else can you do when you examine the heap.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
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)
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
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.
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.
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.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
380C Lecture 17 Where are we & where we are going –Managed languages Dynamic compilation Inlining Garbage collection –Why you need to care about workloads.
Ulterior Reference Counting: Fast Garbage Collection without a Long Wait Author: Stephen M Blackburn Kathryn S McKinley Presenter: Jun Tao.
Fast Conservative Garbage Collection Rifat Shahriyar Stephen M. Blackburn Australian National University Kathryn S. M cKinley Microsoft Research.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Copyright (c) 2004 Borys Bradel Myths and Realities: The Performance Impact of Garbage Collection Paper: Stephen M. Blackburn, Perry Cheng, and Kathryn.
How’s the Parallel Computing Revolution Going? 1How’s the Parallel Revolution Going?McKinley Kathryn S. McKinley The University of Texas at Austin.
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.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
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.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Introduction to Garbage Collection. GC Fundamentals Algorithmic Components AllocationReclamation 2 Identification Bump Allocation Free List ` Tracing.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Immix: A Mark-Region Garbage Collector Jennifer Sartor CS395T Presentation Mar 2, 2009 Thanks to Steve for his Immix presentation from
Garbage Collection What is garbage and how can we deal with it?
Dynamic Compilation Vijay Janapa Reddi
Dynamic Memory Allocation
Rifat Shahriyar Stephen M. Blackburn Australian National University
Storage Management.
Concepts of programming languages
Automatic Memory Management/GC
Automatic Memory Management/GC
Ulterior Reference Counting Fast GC Without The Wait
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Strategies for automatic memory management
Chapter 12 Memory Management
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Automating Memory Management
Run-time environments
Reference Counting.
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Reference Counting vs. Tracing
Presentation transcript:

Memory Management Kathryn McKinley

Isn’t GC a bit retro? Mark-Sweep McCarthy, 1960 Mark-Compact Styger, 1967 Semi-Space Cheney, 1970 “Languages without automated garbage collection are getting out of fashion. The chance of running into all kinds of memory problems is gradually outweighing the performance penalty you have to pay for garbage collection.” Paul Jansen, managing director of TIOBE Software, in Dr Dobbs, April 2008 McKinley, UT 395T MM

Course Logistics Syllabus Critical reading & writing Presentations Discussion Critiques Schedule Volunteers for next week? McKinley, UT 395T MM

Outline Briefly introduce the challenges and key ideas in memory management Context – modern VMs Explicit vs automatic Memory organization Allocation Garbage Identification Reclamation McKinley, UT 395T MM

Class Loader Verifier, etc. Basic VM Structure Program/Bytecode Executing Program Class Loader Verifier, etc. Heap Thread Scheduler Interpreter &/or Dynamic Compiler Garbage Collector McKinley, UT 395T MM

Dynamic memory allocation and reclamation Heap contains dynamically allocated objects Object allocation: malloc, new Deallocation: Manual/explicit: free, delete automatic: garbage collection McKinley, UT 395T MM

Memory Management Objects/data in heap memory How does the runtime system efficiently create and recycle memory on behalf of the program? What makes this problem important? What makes this problem hard? Why are researchers still working on it? McKinley, UT 395T MM

Explicit memory management challenges More code to maintain Correctness Free an object too soon - core dump Free an object too late - waste space Never free - at best waste, at worst fail Efficiency can be very high Gives programmers “control” McKinley, UT 395T MM

Garbage collection: Automatic memory management Reduces programmer burden Eliminates sources of errors which ones? Integral to modern object-oriented languages Java, C#, PHP, JavaScript Mainstream Challenge: performance McKinley, UT 395T MM

Why use Garbage Collection? Software engineering benefits Less user code compared to expilict memory management (MM) Less user code to get correct Protects against some classes of memory errors No free(), thus no premature free(), no double free(), or forgetting to free() Not perfect, memory can still leak Programmers still need to eliminate all pointers to objects the program no longer needs McKinley, UT 395T MM

Why use Garbage Collection? Performance: space/time tradeoff Time proportional to dead objects (explicit mm, reference counting) or live objects (semi-space, mark-sweep) Throughput versus pause time Less frequent collection typically reduces total time but increases space requirements and pause times Hidden locality benefits? Layer of abstraction What else can the collector do when it visits all objects? McKinley, UT 395T MM

Key Issues For both Garbage Collection Fast allocation Fast reclamation Low fragmentation (wasted space) How to organize the memory space Garbage Collection Discriminating live objects and garbage McKinley, UT 395T MM

What is Garbage? McKinley, UT 395T MM

Perfect live object detection Live object has a future use Prove that object is not live, and deallocate it Deallocate as soon as possible after last use McKinley, UT 395T MM

Estimating liveness in practice Approximate liveness by reachability from outside the heap An unreachable object cannot ever be used---it is garbage Once dead always dead! Find and preserve reachable objects Tracing or reference counting Recycle the space of garbage objects McKinley, UT 395T MM

How does the GC implement reachability? McKinley, UT 395T MM

How does the GC implement reachability? Tracing Counting McKinley, UT 395T MM

How does the GC find the pointers to trace or count? Managed languages couple GC with safe pointers Programs may not access arbitrary addresses in memory Compiler can identify and provide the GC with all the pointers….enforcing: “Once garbage, always garbage” Runtime system can move objects by updating pointers Unsafe languages can do non-moving GC by assuming anything that looks like a pointer is one. McKinley, UT 395T MM

Reachability with tracing Compiler produces a stack-map at GC safe-points and Type Information Blocks GC safe points: new(), method entry, method exit, & back-edges (thread switch points) Stack-map: enumerate global variables, stack variables, live registers -- This code is hard to get right! Why? Type Information Blocks: identify reference fields in objects A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Compiler produces a stack-map at GC safe-points and Type Information Blocks Type Information Blocks: identify reference fields in objects for each type i (class) in the program, a map TIBi 2 3 A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them mark A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them mark A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them mark A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them All unmarked objects are dead, and can be reclaimed mark A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Reachability with tracing Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them All unmarked objects are dead, and can be reclaimed sweep A B C { .... r0 = obj PC -> p.f = obj globals stack registers heap McKinley, UT 395T MM

Taxonomy of GC design choices Heap Organization Incrementality Composability Concurrency Parallelism Distribution McKinley, UT 395T MM

Heap organization & basic algorithmic components Allocation Identification Reclamation Sweep-to-Free ` Tracing (implicit) Free List Compact Evacuate Reference Counting (explicit) Bump Allocation A GC algorithm can be broken down into of three algorithmic components. How objects are allocated, how garbage is identified, and how space is reclaimed. 3 1 Sweep-to-Region McKinley, UT 395T MM

One Big Heap? Incrementality Pause times it takes too long to trace the whole heap at once Throughput the heap contains lots of long lived objects, why collect them over and over again? Incremental collection divide up the heap into increments and collect one at a time. to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Incremental Collection Ideally perfect pointer knowledge of live pointers between increments requires scanning whole heap, defeats the purpose to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Incremental Collection Ideally perfect pointer knowledge of live pointers between increments requires scanning whole heap, defeats the purpose to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Incremental Collection Ideally perfect pointer knowledge of live pointers between increments requires scanning whole heap, defeats the purpose to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Incremental Collection Ideally perfect pointer knowledge of live pointers between increments requires scanning whole heap, defeats the purpose Mechanism: Write barrier records pointers between increments when the mutator installs them, conservative approximation of reachability to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier Increment 1 Increment 2 compiler inserts code that records pointers between increments when the mutator installs them // original program // compiler support for incremental collection p.f = o; if (incr(p) != incr(o) { remembered set (incr(o)) U p.f; } p.f = o; remset1 ={w} remset2 ={f,g} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier Increment 1 Increment 2 Install new pointer d -> v // original program // compiler support for incremental collection p.f = o; if (incr(p) != incr(o) { remembered set (incr(o)) U p.f; } p.f = o; remset1 ={w} remset2 ={f,g} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier Increment 1 Increment 2 Install new pointer d -> v, then update d-> y // original program // compiler support for incremental collection p.f = o; if (incr(p) != incr(o) { remembered set (incr(o)) = p.f; } p.f = o; remset1 ={w} remset2 ={f,g,d} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier Increment 1 Increment 2 Install new pointer d -> v, then update d-> y // original program // compiler support for incremental collection p.f = o; if (incr(p) != incr(o) { remembered set (incr(o)) = p.f; } p.f = o; remset1 ={w} remset2 ={f,g,d,d} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier At collection time collector re-examines all entries in the remset for the increment, treating them like roots Collect Increment 2 remset1 ={w} remset2 ={f,g,d,d} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Write barrier At collection time collector re-examines all entries in the remset for the increment, treating them like roots Collect Increment 2 remset1 ={w} remset2 ={f,g,d,d} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Summary of the costs of incremental collection write barrier to catch pointer stores crossing boundaries remsets to store crossing pointers processing remembered sets at collection time excess retention remset1 ={w} remset2 ={f,g,d,d} a b c d e f g t u v w x y z to space from space to space from space Increment 1 Increment 2 McKinley, UT 395T MM

Taxonomy of Design Choices Incrementality Composability Concurrency Parallelism Distribution McKinley, UT 395T MM

GC Ideas Generational collection - Young objects die fast Older first - The longer you wait Garbage first Immix Reference counting - Deferred & Ulterior Concurrent collection [Steele, Djkistra et al.’78] at the same time as the mutator Parallel collection Real time make pause times tiny [Metronome, Petrank et al.] What else can you do when you visit all the objects? Memory utilization & fragmentation Leaks McKinley, UT 395T MM

Questions?