Memory Management and Garbage Collection Hal Perkins Autumn 2011

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

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.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
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.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Generational Stack Collection And Profile driven Pretenuring Perry Cheng Robert Harper Peter Lee Presented By Moti Alperovitch
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.
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.
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.
380C Lecture 17 Where are we & where we are going –Managed languages Dynamic compilation Inlining Garbage collection –Why you need to care about workloads.
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.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
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.
Fast Conservative Garbage Collection Rifat Shahriyar Stephen M. Blackburn Australian National University Kathryn S. M cKinley Microsoft Research.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Copyright (c) 2004 Borys Bradel Myths and Realities: The Performance Impact of Garbage Collection Paper: Stephen M. Blackburn, Perry Cheng, and Kathryn.
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.
September 11, 2003 Beltway: Getting Around GC Gridlock Steve Blackburn, Kathryn McKinley Richard Jones, Eliot Moss Modified by: Weiming Zhao Oct
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.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
® 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.
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Dynamic Compilation Vijay Janapa Reddi
CSE 374 Programming Concepts & Tools
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
Storage Management.
Concepts of programming languages
Automatic Memory Management/GC
Automatic Memory Management
Automatic Memory Management/GC
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Closure Representations in Higher-Order Programming Languages
Beltway: Getting Around Garbage Collection Gridlock
Chapter 12 Memory Management
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Memory Management and Garbage Collection Hal Perkins Autumn 2011 CSE P 501 – Compilers Memory Management and Garbage Collection Hal Perkins Autumn 2011 11/30/2018 © 2002-11 Hal Perkins & UW CSE

References Uniprocessor Garbage Collection Techniques Wilson, IWMM 1992 (longish survey) The Garbage Collection Handbook Jones, Hosking, Moss, 2012 (book) Adapted from slides by Vijay Menon, CSE 501, Sp09 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Program Memory Typically divided into 3 regions: Global / Static: fixed-size at compile time; exists throughout program lifetime Stack / Automatic: per function, automatically allocated and released (local variables) Heap: Explicitly allocated by programmer (malloc/new/cons) Need to recover storage for reuse when no longer needed 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Manual Heap Management Programmer calls free/delete when done with storage Pro Cheap Precise Con How do we enumerate the ways? Buggy, huge debugging costs, … 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Garbage Collection Automatically reclaim heap memory no longer in use by the program Simplify programming Better modularity, concurrency Avoids huge problems with dangling pointers Almost required for type safety But not a panacea – still need to watch for stale pointers, GC’s version of “memory leaks” 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Heap Characteristics Most objects are small (< 128 bytes) Object-oriented and functional code allocates a huge number of short-lived objects Want allocation, recycling to be fast and low overhead Serious engineering required 11/30/2018 © 2002-11 Hal Perkins & UW CSE

What is Garbage? An object is live if it is still in use Need to be conservative OK to keep memory no longer in use Not ok to reclaim something that is live An object is garbage if it is not live 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Reachability Root set : the set of global and local (stack/register) variables visible to active procedures Heap objects are reachable if: They are directly accessible from the root set They are accessible from another reachable heap object (pointers/references) Liveness implies reachability (conservative approximation) Not reachable implies garbage 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Reachability Compiler produces: A stack-map at GC safe points Stack map: enumerate global variables, stack variables, live registers (tricky stuff! Why?) GC safe points: new(), method entry, method exit, back edges (thread switch points) Type information blocks Identifies reference fields in objects (to trace the heap) 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Tracing Collectors Mark the objects reachable from the root set, then perform a transitive closure to find all reachable objects All unmarked objects are dead and can be reclaimed Various algorithms: mark-sweep, copying, generational… 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Mark-Sweep Allocation Multiple free lists organized by size for small objects (8, 16, 24, 32, … depends on alignment); additional list for large blocks Regular malloc does exactly the same Allocation Grab a free object from the right free list No more memory of the right size triggers a collection 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Mark-Sweep Collection Mark phase – find the live objects Transitive closure from root set marking all live objects Sweep phase Sweep memory for unmarked objects and return to appropriate free list(s) 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Mark-Sweep Evaluation Pro Space efficiency Incremental object reclamation Con Relatively slower allocation time Poor locality of objects allocated at around the same time Redundant work rescanning long-lived objects “Stop the world I want to collect” 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Semispace Copying Collector Idea: Divide memory in half Storage allocated from one half of memory When full, copy live objects from old half (“from space”) to unused half (“to space”) & swap semispaces Fast allocation – next chunk of to-space Requires copying collection of entire heap when collection needed 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Semispace collection Same notion of root set and reachable as in mark-sweep collector Copy each object when first encountered Install forwarding pointers in from-space referring to new copy in to-space Transitive closure: follow pointers, copy, and update as it scans Reclaims entire “from space” in one shot Swap from- and to-space when copy done 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Semispace Copying Collector Evaluation Pro Fast allocation Locality of objects allocated at same time Locality of objects connected by pointers (can use depth-first or other strategies during the mark-copy phase) Con Wastes half of memory Redundant work rescanning long-lived objects “Stop the world I want to collect” 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Generational Collectors Generational hypothesis: young objects die more quickly than older ones (Lieberman & Hewitt ‘83, Ungar ‘84) Most pointers are from younger to older objects (Appel ‘89, Zorn ‘90) So, organize heap into young and old regions, collect young space more often 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Generational Collector Divide heap into two spaces: young, old Allocate new objects in young space When young space fills up, collect it and copy surviving objects to old space Engineering: use barriers to avoid having to scan all of old space on quick collections Refinement: require objects to survive at least a few collections before copying When old space fills, collect both Can generalize to multiple generations 11/30/2018 © 2002-11 Hal Perkins & UW CSE

GC Tradeoffs Performance Mutator performance Mark-sweep often faster than semispace Generational better than both Mutator performance Semispace is often fastest Generational is better than mark-sweep Overall: generational is a good balance But: we still “stop the world” to collect 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Open Research Areas Parallel/concurrent garbage collection Found in some production collectors now Tricky stuff – can’t debug it into correctness – there be theorems here Locality issues Object collocation GC-time analysis Distributed GC 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Compiler & Runtime Support GC tightly coupled with safe runtime (e.g., Java, CLR, functional languages) Total knowledge of pointers (type safety) Tagged objects with type information Compiler maps for information Objects can be moved; forwarding pointers 11/30/2018 © 2002-11 Hal Perkins & UW CSE

What about unsafe languages? (e.g., C/C++) Boehm/Weiser collector: GC still possible without compiler/runtime cooperation(!) If it looks like a pointer, it’s a pointer Mark-sweep only – GC doesn’t move anything Allows GC in C/C++ but constraints on pointer bit-twiddling 11/30/2018 © 2002-11 Hal Perkins & UW CSE

Boehm/Weiser Collector Useful for development/debugging Less burden on compiler/runtime implementor Used in various Java and .net implementations Similar ideas for various tools to detect memory leaks, etc. 11/30/2018 © 2002-11 Hal Perkins & UW CSE

And a bit of perspective… Automatic GC has been around since LISP I in 1958 Ubiquitous in functional and object-oriented programming communities for decades Mainstream since Java(?) (mid-90s) Now conventional wisdom? 11/30/2018 © 2002-11 Hal Perkins & UW CSE