Spring 2014Jim Hogg - UW - CSE P501W-1 CSE P501 – Compiler Construction Conventional Heap Storage Garbage Collection.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
1 Overview Assignment 5: hints  Garbage collection Assignment 4: solution.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
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.
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
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.
Generational Stack Collection And Profile driven Pretenuring Perry Cheng Robert Harper Peter Lee Presented By Moti Alperovitch
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
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.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
CLR: Garbage Collection Inside Out
COP4020 Programming Languages
1 Thread Synchronization (Un)Fairness in the.NET Common Language Runtime If you are building an application that absolutely requires fair thread synchronization,
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.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
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.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Problem of the Day  Why are manhole covers round?
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
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.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
GC Assertions: Using the Garbage Collector To Check Heap Properties Samuel Z. Guyer Tufts University Edward Aftandilian Tufts University.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Object Lifetime and Pointers
Storage Allocation Mechanisms
Module 9: Memory and Resource Management
CSE 374 Programming Concepts & Tools
Memory Management © 2004, D. J. Foreman.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Optimizing Malloc and Free
Storage.
Simulated Pointers.
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Strategies for automatic memory management
Topic 3-b Run-Time Environment
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Automating Memory Management
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Spring 2014Jim Hogg - UW - CSE P501W-1 CSE P501 – Compiler Construction Conventional Heap Storage Garbage Collection

Spring 2014Jim Hogg - UW - CSE P501W-2... char* s = (char*) malloc(50);... free(s); C C Runtime Heap Memory Developer must remember to free memory when no longer required Eventual fragmentation => slow to malloc, slow to free In Use Conventional Heap Storage

Spring 2014Jim Hogg - UW - CSE P501W-3 Heap Storage Fragmentation C Runtime Heap Memory In Use 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

Spring 2014Jim Hogg - UW - CSE P501W-4 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!

Spring 2014Jim Hogg - UW - CSE P501W-5 Solving Memory Leaks int f() {... Car c = Car("Chevy");... } C++ object c is destructed [calls ~Car() ]automatically when c it falls out of scope called RAII ("Resource Acquisition Is Initialization") more general case still requires Developer to delete c when no longer needed "smart pointers" can help

W-6 Solving Leaks and Use-After-Free public static int Main(String[] args) {... if (...) { ArrayList cars = new ArrayList(); for (int i = 0; i < 1000; i++) { cars.Add(new Car()); }... }... Boat b = new Boat();... } A At point A I have 1000 Car objects allocated on the heap At some later time I ask for more heap memory. May trigger a "garbage collection" - automatic, and silent Compiler realizes that cars is not referenced later by the program; so it's "garbage"; GC recycles it for use by other objects Magically solves leaks and use-after-free! B

Spring 2014Jim Hogg - UW - CSE P501W-7 next Allocate an object; fast! next Allocate more objects; and one more, please? Garbage Collection

Spring 2014Jim Hogg - UW - CSE P501W-8 Garbage Collection, 1 Allocate another object next "roots" Trace reachable objects next "roots" Compact unreachables; update all pointers GC does not find garbage: it finds live objects and ignores all other memory

Spring 2014Jim Hogg - UW - CSE P501W-9 Roots? "Roots" are locations that hold a pointer to any object in the heap: Method-local variables Method Arguments Global variables Class-static fields Registers

Spring 2014Jim Hogg - UW - CSE P501W-10 GC Start root

Spring 2014Jim Hogg - UW - CSE P501W-11 GC Mark Phase root Unreachable Reachable

Spring 2014Jim Hogg - UW - CSE P501W-12 GC Sweep Phase root Reachable With memory free, now allocate space for object that provoked the GC

Spring 2014Jim Hogg - UW - CSE P501W-13 No Bugs Forget to free => eventually run out of memory called a "memory leak" Call free, but continue to use! called "use-after-free" GC removes control over free'ing memory from the hands of the Developer GC will find all live objects and reclaim all other memory each time it is invoked. So no "memory leak"s An object is garbage-collected only if it was unreachable by the program; being unreachable, GC removes "use-after-free" bugs So what's a "memory leak" in C# or Java? - holding on to objects that really could be freed - eg, by setting the object-ref to null. Particularly troublesome in a Generational Garbage Collector

Spring 2014Jim Hogg - UW - CSE P501W-14 C# Finalizers Class C above defines a "Finalize" method. Syntax is ~C() Syntactic sugar for: protected override void Finalize() As each object of class C is created, the CLR links it onto a Finalization Queue When the object is about to be reclaimed, it is moved onto the Freachable Queue; a Finalizer background thread will run that Finalize method later class C { private StreamWriter sw;... ~C() { sw.Close(); }// flush output buffer... }

Spring 2014Jim Hogg - UW - CSE P501W-15 Finalization root Finalization Queue root Finalization Queue Freachable Queue Not live; not dead; zombies

Spring 2014Jim Hogg - UW - CSE P501W-16 Threads Compiler needs to create GC info - at each point in program, where are current roots? - variables, registers GC info is bulky - cannot store for every instruction! So store GC info for selected points in the program - "GC-safepoints" Typical GC-safepoint is a function return Need to stop all threads to carry out a GC For each thread stack-frame, stomp its return address; when it reaches there, the thread will return, not to its caller, but into GC code

Spring 2014Jim Hogg - UW - CSE P501W-17 Hijacking a Thread retaddrgc caller GC gc GC gc GC Top of Stack Save & Stomp retaddr Thread continues to run Eventually returns and hits the hijack

Spring 2014Jim Hogg - UW - CSE P501W-18 Threads Run to Their Safepoints Start GC After each thread has hit its safepoint, GC can start its sweep phase

Spring 2014Jim Hogg - UW - CSE P501W-19 Object Age Distribution Performing GC over large heap consumes lots of cpu time (and trashes the caches!) Experiment reveals: Many objects live a very short time (high "infant mortality") Some object live a very long time Bi-modal number age Object Ages

Spring 2014Jim Hogg - UW - CSE P501W-20 Generational Garbage Collection Gen0Gen2Gen1 'Nursery' Divide heap into 3 areas Allocate new objects from Gen0 At next GC move all Gen0 survivors into Gen1 move all Gen1 survivors into Gen2

Spring 2014Jim Hogg - UW - CSE P501W-21 Migration thru Generations Gen0Gen2Gen1 Gen0Gen2Gen1 Gen0 0Gen2Gen1 Survivors Gen0Gen2Gen1

Spring 2014Jim Hogg - UW - CSE P501W-22 Generational GC Garbage-collecting entire heap is expensive A process of diminishing returns Instead, Garbage-Collect only the nursery (Gen0) Smaller, so faster Yields lots of free space (objects die young) Maximum 'bang for the buck' Sometime collect Gen0+Gen1 When the going gets tough, collect Gen0+Gen1+Gen2 ("full" collection) Some old objects may become unreachable - just ignore them in a Gen0 collect Above picture assumes no writes to old objects kept Gen0 objects reachable

Spring 2014Jim Hogg - UW - CSE P501W-23 Card Table In practice, older objects are written-to between GCs Might update a pointer that results in keeping young object alive; missing this is a disaster Gen0Gen2Gen1 Card Tables: bitmap, with 1 bit per 128 Bytes of Gen2 heap Create a "write barrier" - any write into Gen1 or Gen2 heap sets corresponding bit in Card Table During GC sweep, check Card Tables for any "keep alive" pointers In practice, Card Tables contain few 1 bits Gen2 Card Table Gen1 Card Table

Spring 2014Jim Hogg - UW - CSE P501W-24 Debugging the GC What if a GC goes wrong? Fails to free some memory that is unreachable => memory leak Frees some memory that is still reachable - impending disaster Frees some non-objects - disaster Colloquially called a GC 'hole' Causes Bug in GC - mark, sweep, compaction Bug in GC info Symptoms Wrong answers Process Crash If we're 'lucky' failure comes soon after error Otherwise, failure may occur seconds, minutes or hours later Testing GC-Stress eg: maximally randomize the heap; force frequent GCs; check results

Spring 2014Jim Hogg - UW - CSE P501W-25 Further Design Complexities Objects with Finalizers: take 2 GCs to die extend the lifetime of other connected objects run at some later time => "non-deterministic" finalization no guarantee on order that finalizers runs uncaught exceptions escape and disappear! Strong refs Weak refs - "short" and "long" "Dispose" pattern Resurrection Large Object Heap (objects > 85 kB in size) Concurrent Garbage Collection Per-processor heaps; processor affinity Fully-interruptible GC Safe-point on back-edge of loop

Spring 2014Jim Hogg - UW - CSE P501W-26 References for CLR GC Garbage Collection: Automatic Memory Management in the Microsoft.NET Framework – by Jeffrey Richter Garbage Collection—Part 2: Automatic Memory Management in the Microsoft.NET Framework - by Jeffrey Richter Garbage Collector Basics and Performance Hints – by Rico Mariani Using GC Efficiently – Part 1 by Maoni Using GC Efficiently – Part 2 by Maoni Using GC Efficiently – Part 3 by Maoni Using GC Efficiently – Part 4 by Maoni GC Performance Counters - by Maoni Tools that help diagnose managed memory related issues – by Maoni Clearing up some confusion over finalization and other areas in GC – by Maoni

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? - nope! Specialized patterns of allocate/free are still better coded by- hand - eg "Arena" storage inside compiler optimization phases Spring 2014Jim Hogg - UW - CSE P501W-27