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

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

Managed runtimes & garbage collection

Lecture 13: Garbage Collection

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

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Garbage Collection. Hwansoo Han

Garbage Collection (1)

Automatic Memory Management

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

CS 241 Honors Memory

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

Garbage Collection Algorithms. Ganesh Bikshandi

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

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

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

Lecture 15 Garbage Collection

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

Compiler Construction D7011E

Programming Language Implementation

CS61C : Machine Structures

Binghamton University Dynamic Memory Alloca/on

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Garbage Collection. Weiyuan Li

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

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.

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

Run-Time Environments/Garbage Collection

Run-time Environments -Part 3

Name, Scope, and Binding. Outline [1]

Garbage Collection Techniques

Implementation Garbage Collection

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

Automatic Garbage Collection

CS61C : Machine Structures

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Exploiting the Behavior of Generational Garbage Collector

Lecture Notes on Garbage Collection

Habanero Extreme Scale Software Research Project

Garbage Collection. Vyacheslav Egorov

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Heap Management. Heap Allocation

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

Dynamic Memory Alloca/on: Advanced Concepts

CS61C : Machine Structures

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

CS61C : Machine Structures

Robust Memory Management Schemes

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

Lecture Outine. Why Automatic Memory Management? Garbage Collection. Automatic Memory Management. Three Techniques. Lecture 18

Structure of Programming Languages Lecture 10

I J C S I E International Science Press

Memory Management. CS449 Fall 2017

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

Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Simple Garbage Collection and Fast Allocation Andrew W. Appel

Dynamic Memory Alloca/on: Advanced Concepts

Dynamic Storage Allocation

Garbage Collection. Steven R. Bagley

Lecture Notes on Garbage Collection

Dynamic Memory Management

Dynamic Memory Management

Lecture 2: Memory in C

Compiler Construction

Dynamic Memory Management! Goals of this Lecture!

Compiler Construction

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

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

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

Dynamic Memory Alloca/on: Basic Concepts

Run-time Environments - 3

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014

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

Lecture Notes on Advanced Garbage Collection

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

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Baggy bounds checking. Periklis Akri5dis, Manuel Costa, Miguel Castro, Steven Hand

G Programming Languages - Fall 2012

Garbage Collection (2) Advanced Operating Systems Lecture 9

CS61C : Machine Structures

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

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

Myths and Realities: The Performance Impact of Garbage Collection

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

Mark-Sweep and Mark-Compact GC

A new Mono GC. Paolo Molaro October 25, 2006

Memory Allocation III

CS842: Automatic Memory Management and Garbage Collection. GC basics

CS61C : Machine Structures

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Lecture 15 Advanced Garbage Collection

Opera&ng Systems CMPSCI 377 Dynamic Memory Management. Emery Berger and Mark Corner University of Massachuse9s Amherst

(notes modified from Noah Snavely, Spring 2009) Memory allocation

Dynamic Memory Allocation

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

ECE 598 Advanced Operating Systems Lecture 10

Hard Real-Time Garbage Collection in Java Virtual Machines

CS Computer Systems. Lecture 8: Free Memory Management

Transcription:

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

References Uniprocessor Garbage Collec1on Techniques Wilson, IWMM 1992 (longish survey) The Garbage Collec1on Handbook Jones, Hosking, Moss, 2012 (book) Earlier version of this lecture by Vijay Menon, CSE 501, Sp09; Jim Hogg, CSE P 501 Sp14 UW CSE P 501 Winter 2016 W-2

Program Memory Typically divided into 3 regions: Global / Sta<c: fixed-size at compile <me; exists throughout program life<me Stack / Automa<c: per func<on, automa<cally allocated and released (local variables) Heap: Explicitly allocated by programmer (malloc/ new/cons) Need to recover / recycle storage for reuse when no longer needed UW CSE P 501 Winter 2016 W-3

Manual Heap Management Programmer calls free/delete when done with storage Pro Cheap Precise Con How do we enumerate the ways? the pain? Buggy, huge debugging costs, UW CSE P 501 Winter 2016 W-4

Conven<onal Heap Storage... char* s = (char*) malloc(50);... free(s); In Use C Runtime Heap Memory Developer must remember to free memory when no longer required Eventual fragmentation => slow to malloc, slow to free UW CSE P 501 Winter 2016 W-5

Heap Storage Fragmenta<on In Use C Runtime Heap Memory malloc: walk the freelist to find a slot big enough for current request free: adjust freelist; collapse contiguous freespace fragmentation: plenty free chunks but none big enough for request cannot compact the used space - may contain pointers; may be pointed-at UW CSE P 501 Winter 2016 W-6

Bugs Forget to free => eventually run out of memory called a "memory leak Call free, but continue to use! called "use-after-free", or "dangling pointer" memory corruption - wrong answers; crash if lucky! major source of security issues detect via "pool poisoning" 2 pointers free via 1 free malloc; corruption! UW CSE P 501 Winter 2016 W-7

Garbage Collec<on Automa<cally reclaim heap memory no longer in use by the program Simplify programming Be`er modularity, concurrency Avoids huge problems with dangling pointers Almost required for type safety But not a panacea s<ll need to watch for stale pointers, GC s version of memory leaks i.e., pointers in live data to no-longer-used data UW CSE P 501 Winter 2016 W-8

Garbage Collec<on next Allocate an object; fast! next Allocate more objects; and one more, please? next UW CSE P 501 Winter 2016 W-9

Garbage Collec<on Allocate another object "roots" next Trace reachable objects "roots" next next Compact unreachables; update all pointers GC does not find garbage: it finds live objects and ignores all other memory UW CSE P 501 Winter 2016 W-10

Heap Characteris<cs Most objects are small (< 128 bytes) Object-oriented and func<onal code allocates a huge number of short-lived objects Want alloca<on, recycling to be fast and low overhead Serious engineering required UW CSE P 501 Winter 2016 W-11

Alloca<on Usually mul<ple free lists organized by size for small objects (8, 16, 24, 32, depends on alignment); addi<onal list for large blocks Regular malloc does exactly the same Alloca<on Grab a free object from the right free list No more memory of the right size triggers a collec<on UW CSE P 501 Winter 2016 W-12

What is Garbage? An object is live if it is s<ll in use GC needs to be conserva<ve 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 UW CSE P 501 Winter 2016 W-13

Reachability Root set : the set of global and local (stack + register) variables visible to ac<ve 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 (conserva<ve approxima<on) Not reachable implies garbage UW CSE P 501 Winter 2016 W-14

Tracing Collectors Mark the objects reachable from the root set, then perform a transi<ve closure to find all reachable objects All unmarked objects are dead and can be reclaimed Various algorithms: mark-sweep, copying, genera<onal UW CSE P 501 Winter 2016 W-15

Mark-Sweep Collec<on Mark phase find the live objects Transi<ve closure from root set marking all live objects Sweep phase Sweep memory for unmarked objects and return to appropriate free list(s) UW CSE P 501 Winter 2016 W-16

GC Start root root UW CSE P 501 Winter 2016 W-17

GC Mark Phase root root Unreachable Reachable UW CSE P 501 Winter 2016 W-18

GC Sweep Phase root root Reachable With memory free, now allocate space for object that provoked the GC UW CSE P 501 Winter 2016 W-19

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) Stop all threads at one of their GC safe points and then ok to do a collec<on Type informa1on blocks Iden<fies reference fields in objects (to trace the heap) UW CSE P 501 Winter 2016 W-20

Mark-Sweep Evalua<on Pro Space efficiency Incremental object reclama<on Con Rela<vely slower alloca<on <me (free lists vs. next chunk of heap ) Can have poor locality of objects allocated at around the same <me Redundant work rescanning long-lived objects Stop the world I want to collect UW CSE P 501 Winter 2016 W-21

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 alloca<on next chunk of to-space Requires copying collec<on of en<re heap when collec<on needed UW CSE P 501 Winter 2016 W-22

Semispace collec<on Same no<on 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 Transi<ve closure: follow pointers, copy, and update as it scans Reclaims en<re from space in one shot Swap from- and to-space when copy done UW CSE P 501 Winter 2016 W-23

Semispace Copying Collector Evalua<on Pro Fast alloca<on Locality of objects allocated at same <me Locality of objects connected by pointers (can use depthfirst or other strategies during the mark-copy phase) Con Wastes half of (virtual?) memory Other copying/compac<ng collectors solve some of this Be careful with VM don t want compac<ng to thrash Redundant work rescanning long-lived objects Stop the world I want to collect UW CSE P 501 Winter 2016 W-24

Genera<onal Collectors Genera<onal hypothesis: young objects die more quickly than older ones (Lieberman & Hewi` 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 oten UW CSE P 501 Winter 2016 W-25

Genera<onal 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 write barriers to avoid having to scan all of old space on quick collec<ons most pointers that cross the boundary are from young objects to old Refinement: require objects to survive at least a few collec<ons before copying When old space fills, collect both Oten use mul<ple genera<ons, not just two UW CSE P 501 Winter 2016 W-26

GC Tradeoffs Performance Mark-sweep oten faster than semispace Genera<onal be`er than both Mutator performance Semispace is oten fastest Genera<onal is be`er than mark-sweep Overall: genera<onal is a good balance But: we s<ll stop the world to collect UW CSE P 501 Winter 2016 W-27

Advanced GC and Research Areas Parallel/concurrent garbage collec<on Found in some produc<on collectors Tricky stuff can t debug it into correctness there be theorems here Locality issues Object colloca<on GC-<me analysis Distributed GC UW CSE P 501 Winter 2016 W-28

Compiler & Run<me Support GC <ghtly coupled with safe run<me (e.g., Java, CLR, func<onal languages) Total knowledge of pointers (type safety) Tagged objects with type informa<on Compiler maps for informa<on Objects can be moved; forwarding pointers UW CSE P 501 Winter 2016 W-29

What about unsafe languages? (e.g., C/C++) Boehm/Weiser collector: GC s<ll possible without compiler/run<me coopera<on(!) New versions of malloc (& free) + GC to manage heap 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 bittwiddling Surprisingly effec<ve, par<cularly if program uses pointers as in a type-safe language (e.g., no pointer mangling, no (void*)int tricks, etc.) UW CSE P 501 Winter 2016 W-30

Boehm/Weiser Collector Useful for development/debugging Less burden on compiler/run<me implementor Used in various Java and.net prototypes, research implementa<ons, produc<on code if sufficiently effec<ve Similar ideas for various tools to detect memory leaks, etc. UW CSE P 501 Winter 2016 W-31

A bit of perspec<ve Automa<c GC has been around since LISP I in 1958 Ubiquitous in func<onal and object-oriented programming communi<es for decades Mainstream since Java(?) (mid-90s) Now conven<onal wisdom? UW CSE P 501 Winter 2016 W-32