Compilation 0368-3133 (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.

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

Automatic Memory Management Noam Rinetzky Schreiber 123A /seminar/seminar1415a.html.
Compilation (Semester A, 2013/14) Lecture 14: Compiling Object Oriented Programs Noam Rinetzky Slides credit: Mooly Sagiv 1.
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
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.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.
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.
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++)
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
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.
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Theory of Compilation Erez Petrank Lecture 9: Runtime (part 2); object oriented issues 1.
Garbage Collection Mooly Sagiv html://
Memory Management Chapter 5 Mooly Sagiv
CS 536 Spring Run-time organization Lecture 19.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
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.
1 An Efficient On-the-Fly Cycle Collection Harel Paz, Erez Petrank - Technion, Israel David F. Bacon, V. T. Rajan - IBM T.J. Watson Research Center Elliot.
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
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.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
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.
Lecture 10 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Compiler Construction
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Theory of Compilation Erez Petrank Lecture 9: Runtime Part II. 1.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
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.
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.
Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.
® 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.
Reference Counting. Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Garbage Collection What is garbage and how can we deal with it?
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Run-time organization
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Strategies for automatic memory management
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1

AST + Sym. Tab. Stages of compilation Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) Assembly IR TextToken streamAST Code Generation

Compilation  Execution AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) IR TextToken streamAST Code Generation Linker Assembler Loader Symbolic Addr Object FileExecutable Fileimage Executing program Runtime System

Runtime Environment Mediates between the OS and the programming language Hides details of the machine from the programmer –Ranges from simple support functions all the way to a full-fledged virtual machine Handles common tasks –Runtime stack (activation records) –Memory management –Dynamic optimization –Debugging –… 4

Where do we allocate data? Activation records –Lifetime of allocated data limited by procedure lifetime –Stack frame deallocated (popped) when procedure return Dynamic memory allocation on the heap 5

Memory Layout stack grows down (towards lower addresses) heap grows up (towards higher addresses) heap stack code static data 6

Alignment Typically, can only access memory at aligned addresses –Either 4-bytes or 8-bytes What happens if you allocate data of size 5 bytes? –Padding – the space until the next aligned addresses is kept empty (side note: x86, is more complicated, as usual, and also allows unaligned accesses, but not recommended) 7

Allocating memory In C - malloc void *malloc(size_t size) Why does malloc return void* ? –It just allocates a chunk of memory, without regard to its type How does malloc guarantee alignment? –After all, you don’t know what type it is allocating for –It has to align for the largest primitive type –In practice optimized for 8 byte alignment (glibc-2.17) 8

Memory Management Manual memory management Automatic memory management 9

Manual memory management malloc free a = malloc(…) ; // do something with a free(a); 10

malloc where is malloc implemented? how does it work? 11

free Free too late – waste memory (memory leak) Free too early – dangling pointers / crashes Free twice – error 12

When can we free an object? a = malloc(…) ; b = a; // free (a); ? c = malloc (…); if (b == c) printf(“unexpected equality”); Cannot free an object if it has a reference with a future use! 13

When can free x be inserted after p? p cannot free x x references an object l some reference to l is used On all execution paths after p there are no uses of references to the object referenced by x  inserting free x after p is valid 14

Automatic Memory Management automatically free memory when it is no longer needed not limited to OO languages prevalent in OO languages such as Java –also in functional languages 15

Garbage collection approximate reasoning about object liveness use reachability to approximate liveness assume reachable objects are live –non-reachable objects are dead 16

Garbage Collection – Classical Techniques reference counting mark and sweep copying 17

GC using Reference Counting add a reference-count field to every object –how many references point to it when (rc==0) the object is non reachable –non reachable => dead –can be collected (deallocated) 18

Managing Reference Counts Each object has a reference count o.RC A newly allocated object o gets o.RC = 1 –why? write-barrier for reference updates update(x,old,new) { old.RC--; new.RC++; if (old.RC == 0) collect(old); } collect(old) will decrement RC for all children and recursively collect objects whose RC reached 0. 19

Cycles! cannot identify non-reachable cycles –reference counts for nodes on the cycle will never decrement to 0 several approaches for dealing with cycles –ignore –periodically invoke a tracing algorithm to collect cycles –specialized algorithms for collecting cycles 20

The Mark-and-Sweep Algorithm [McCarthy 1960] Marking phase –mark roots –trace all objects transitively reachable from roots –mark every traversed object Sweep phase –scan all objects in the heap –collect all unmarked objects 21

The Mark-Sweep algorithm Traverse live objects & mark black. White objects can be reclaimed. registers Roots Note! This is not the heap data structure! 22

Triggering New(A)= if free_list is empty mark_sweep() if free_list is empty return (“out-of-memory”) pointer = allocate(A) return (pointer) Garbage collection is triggered by allocation 23

Basic Algorithm mark_sweep()= for Ptr in Roots mark(Ptr) sweep() mark(Obj)= if mark_bit(Obj) == unmarked mark_bit(Obj)=marked for C in Children(Obj) mark(C) Sweep()= p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p) 24

Mark&Sweep Example r1 r2 25

Mark&Sweep in Depth mark(Obj)= if mark_bit(Obj) == unmarked mark_bit(Obj)=marked for C in Children(Obj) mark(C) How much memory does it consume? –Recursion depth? –Can you traverse the heap without worst-case O(n) stack? Deutch-Schorr-Waite algorithm for graph marking without recursion or stack (works by reversing pointers) 26

Properties of Mark & Sweep Most popular method today Simple Does not move objects, and so heap may fragment Complexity Mark phase: live objects (dominant phase)  Sweep phase: heap size Termination: each pointer traversed once Engineering tricks used to improve performance 27

During the run objects are allocated and reclaimed Gradually, the heap gets fragmented When space is too fragmented to allocate, a compaction algorithm is used Move all live objects to the beginning of the heap and update all pointers to reference the new locations Compaction is very costly and we attempt to run it infrequently, or only partially Mark-Compact The Heap 28

Mark Compact Important parameters of a compaction algorithm –Keep order of objects? –Use extra space for compactor data structures? –How many heap passes? –Can it run in parallel on a multi-processor? We do not elaborate in this intro 29

Copying GC partition the heap into two parts –old space –new space Copying GC algorithm –copy all reachable objects from old space to new space –swap roles of old/new space 30

Example oldnew Roots A D C B E 31

Example oldnew Roots A D C B E A C 32

Properties of Copying Collection Compaction for free Major disadvantage: half of the heap is not used “Touch” only the live objects –Good when most objects are dead –Usually most new objects are dead Some methods use a small space for young objects and collect this space using copying garbage collection 33

A very simplistic comparison CopyingMark & sweepReference Counting Live objectsSize of heap (live objects) Pointer updates + dead objects Complexity Half heap wasted Bit/object + stack for DFS Count/object + stack for DFS Space overhead For freeAdditional work Compaction long Mostly shortPause time Cycle collectionMore issues 34

Parallel Mark&Sweep GC r1 r2 Thread 1 Thread 2 Parallel GC: mutator is stopped, GC threads run in parallel 35

Concurrent Mark&Sweep Example r1 r2 X X Concurrent GC: mutator and GC threads run in parallel, no need to stop mutator 36

Problem: Interference A C B 1. GC traced B SYSTEM = MUTATOR || GC 37

Problem: Interference A C B A C B 1. GC traced B2. Mutator links C to B SYSTEM = MUTATOR || GC 38

Problem: Interference A C B A C B A C B X 1. GC traced B2. Mutator links C to B 3. Mutator unlinks C from A SYSTEM = MUTATOR || GC 39

Problem: Interference A C B A C B A C B A C B C LOST 1. GC traced B2. Mutator links C to B 3. Mutator unlinks C from A 4. GC traced A SYSTEM = MUTATOR || GC 40

The 3 Families of Concurrent GC Algorithms A C B 1. Marks C when C is linked to B (DIJKSTRA) A C B 2. Marks C when link to C is removed (YUASA) X A C B 3. Rescan B when C is linked to B (STEELE) C C B 41

Modern Memory Management Considers standard program properties Handle parallelism –Stop the program and collect in parallel on all available processors –Run collection concurrently with the program run Cache consciousness Real-time 42

Conservative GC How do you track pointers in languages such as C ? –Any value can be cast down to a pointer How can you follow pointers in a structure? Easy – be conservative, consider anything that can be a pointer to be a pointer Practical! (e.g., Boehm collector) 43

Conservative GC Can you implement a conservative copying GC? What is the problem? Cannot update pointers to the new address… you don’t know whether the value is a pointer, cannot update it 44

Terminology Recap Heap, objects Allocate, free (deallocate, delete, reclaim) Reachable, live, dead, unreachable Roots Reference counting, mark and sweep, copying, compaction, tracing algorithms Fragmentation 45

The End 46