CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Ceg860 (Prasad)L9GC1 Memory Management Garbage Collection.
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
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 Management Chapter  Memory management: the process of binding values to (logical) memory locations.  The memory accessible to a program.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Garbage Collection Mooly Sagiv html://
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Management Chapter 5 Mooly Sagiv
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
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.
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
Linked lists and memory allocation Prof. Noah Snavely CS1114
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
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.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
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.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
CSC 533: Programming Languages Spring 2015
Lectures linked lists Chapter 6 of textbook
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
Automatic Memory Management
CSE 3302 Programming Languages
Storage.
Strategies for automatic memory management
Chapter 12 Memory Management
CS703 - Advanced Operating Systems
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CSC 533: Programming Languages Spring 2019
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras

CSE 5317/4305 L11: Storage Allocation2 Heap-Allocated Data The simplest heap allocator that does not reclaim storage –similar to the one used in the project char heap[heap_size]; int end_of_heap = 0; void* malloc ( int size ) { void* loc = (void*) &heap[end_of_heap]; end_of_heap += size; return loc; };

CSE 5317/4305 L11: Storage Allocation3 With a Free List Need to recycle the dynamically allocated data that are not used This is done using free in C or delete in C++ Need to link all deallocated data in the heap into a list Initially, the free list contains only one element that covers the entire heap (ie, it's heap_size bytes long) typedef struct Header { struct Header *next; int size; } Header; Header* free_list; free simply puts the recycled cell at the beginning of the free list: void free ( void* x ) { if ("size of *x" <= sizeof(Header)) return; ((Header*) x)->next = free_list; ((Header*) x)->size = "size of *x"; free_list = (Header*) x; };

CSE 5317/4305 L11: Storage Allocation4 Malloc malloc first searches the free list to find a cell large enough to fit the given number of bytes. If it finds one, it gets a chunk out of the cell leaving the rest untouched: void* malloc ( int size ) { Header* prev = free_list; for (Header* r=free_list; r!=0; prev=r, r=r->next) if (r->size > size+sizeof(Header)) { Header* new_r = (Header*) (((char*) r)+size); new_r->next = r->next; new_r->size = r->size; if (prev==free_list) free_list = new_r; else prev->next = new_r; return (void*) r; }; void* loc = (void*) &heap[end_of_heap]; end_of_heap += size; return loc; };

CSE 5317/4305 L11: Storage Allocation5 Problems with Manual Allocation lots of overhead in malloc since the free list may be very long fragmentation of the heap into tiny cells –even though the total free space in the free list may be plenty, it is useless for large object allocation –improvement: we can keep a vector of free lists, so that the nth element of the vector is a free list that links all the free cells of size n the programmer is responsible for allocating and deleting objects explicitly –it is the source of the worst and hardest to find bugs –it's also the source of most of the mysterious program crashes –it causes horrible memory problems due to “overflow”, “fence past errors”, “memory corruption”, “step-on-others-toe” (hurting other variable's memory locations) or “memory leaks” –the memory problems are extremely hard to debug and are very time consuming to fix and troubleshoot

CSE 5317/4305 L11: Storage Allocation6 Problems (cont.) –memory problems bring down the productivity of programmers –memory related bugs are very tough to crack, and even experienced programmers take several days or weeks to debug memory related problems –memory bugs may be hidden inside the code for several months and can cause unexpected program crashes –a program may work fine in a platform but have memory bugs when ported in a new platform, making programs non-portable –it is estimated that the memory bugs due to usage of char* and pointers in C/C++ is costing $2 billions every year in time lost due to debugging and downtime of programs Why memory management is so hard to do correctly? –you need to have a global view of how dynamic instances of a type are created and passed around –this destroys the good software engineering principle that programs should be developed in small independent components

CSE 5317/4305 L11: Storage Allocation7 Reference Counting Keeping track of pointer assignments If more than one objects point to a dynamically allocated object, then the latter object should be deleted only if all objects that point to it do not need it anymore –you need to keep track of how many pointers are pointing to each object Used to be popular for OO languages like C++ Note: this is not automatic garbage collection because the programmer again is responsible of putting counters to every object to count references This method is easy to implement for languages like C++ where you can redefine the assignment operator dest=source, when both dest and source are pointers to data

CSE 5317/4305 L11: Storage Allocation8 Reference Counting (cont.) Instead of using C++ for a pointer to an object C, we use Ref, where the template Ref provides reference counting to C: template class Ref { private: int count; T* pointer; void MayDelete () { if (count==0) delete pointer; }; void Copy ( const Ref &sp ) { ++sp.count; count = sp.count; pointer = sp.pointer; }; public: Ref ( T* ptr = 0 ) : count(1), pointer(ptr) {}; Ref ( const Ref &sp ) { Copy(sp); }; ~Ref () { MayDelete(); }; T* operator-> () { return pointer; }; Ref& operator= ( const Ref &sp ) { if (this != &sp) { count--; MayDelete(); Copy(sp); }; return *this; };

CSE 5317/4305 L11: Storage Allocation9 Problems with Reference Counting Reference counting avoids some misuses of the heap but it comes with a high cost: –every assignment takes many cycles to be completed and some of the work may be unnecessary since you may pass a pointer around causing many unnecessary counter increments/decrements –we cannot get rid of cyclic objects (eg. when A points to B and B points to A) using reference counting all objects that participate in a cyclic chain of references will always have their counters greater than zero, so they will never be deleted, even if they are garbage

CSE 5317/4305 L11: Storage Allocation10 Automatic Garbage Collection Heap-allocated records that are not reachable by any chain of pointers from program variables are garbage Garbage collection: –a program does not reclaim memory manually –when the heap is full, the run-time system suspends the program and starts garbage collection char heap[heap_size]; int end_of_heap = 0; void* malloc ( int size ) { if (size+end_of_heap > heap_size) GC(); void* loc = (void*) &heap[end_of_heap]; end_of_heap += size; return loc; };

CSE 5317/4305 L11: Storage Allocation11 Not Reachable => Garbage Conservative approximation: –if we can reach an object by following pointers from variables, then the object is live (not garbage) Roots = program variables (frame-allocated or static) –need to check all frames in the run-time stack for pointers to heap –conservative approach: if a word has a value between the minimum and maximum address of the heap, then it is a pointer to the heap An object is live if it is pointed by either a root or by a live object –a garbage collector needs to start from each root and following pointers recursively

CSE 5317/4305 L11: Storage Allocation12 Mark-and-Sweep Collection Two phases: 1)Mark: starting from roots, mark all reachable objects by using a depth- first-search pointer traversal 2)Sweep: scan the heap from the beginning to the end and reclaim the unmarked objects (and unmark the marked objects) DFS ( p ) { if (*p record is unmarked) then { mark *p; for each pointer p->fi of the record *p do DFS(p->fi) } for each p in roots DFS(p) p = 'first object in the heap' while p is in the heap do { if *p is marked then unmark *p else insert *p into the free list p = p+(size of record *p) }

CSE 5317/4305 L11: Storage Allocation13 Example

CSE 5317/4305 L11: Storage Allocation14 Example (cont.) free list

CSE 5317/4305 L11: Storage Allocation15 Pointer Reversal Trick: use the objects themselves as a stack current previous

CSE 5317/4305 L11: Storage Allocation16 Copying Collection Need two heaps –from-space: the current working heap –to-space: needs to be in memory during garbage collection Copying garbage collection: 1)create the to-space heap in memory 2)copy the live objects from the from-space to the to-space ● must make sure that pointers are referring to the to-space (pointer forwarding) 3)dispose the from-space and use the to-space as the new from-space

CSE 5317/4305 L11: Storage Allocation17 Forwarding a Pointer forward (p) { if p points to from-space then if p.f1 points to to-space then return p.f1 else { for each field fi of p do next.fi := p.fi p.f1 := next next.f1 := next next := next + (size of *p) return p.f1 } else return p

CSE 5317/4305 L11: Storage Allocation18 Cheney's Algorithm Breadth-first-search Locality of reference scan := begin-of-to-space next := scan for each root r r := forward(r) while scan < next { for each field fi of *scan scan.fi := forward(scan.fi) scan := scan + (size of *scan) }

CSE 5317/4305 L11: Storage Allocation19 Example

CSE 5317/4305 L11: Storage Allocation20 Forwarding the Roots After we forward the roots from the from-space to the to-space, the to-space will contain the forwarded roots, and the roots and the forward pointers of the root elements in the from-space will point to the to-space

CSE 5317/4305 L11: Storage Allocation21 Example (cont.) Then, we forward the pointers of the first element of the to-space pointed by the scan pointer (element 51). The first pointer 6 has already been forwarded to 52. The second pointer 3 is forwarded at the end of the to-space to 54

CSE 5317/4305 L11: Storage Allocation22 Example (cont.) Now we forward the pointer 8 of element 52

CSE 5317/4305 L11: Storage Allocation23 Example (cont.) and the pointer 7 of element 52

CSE 5317/4305 L11: Storage Allocation24 Example (cont.) Now we forward the pointer 10 of element 53

CSE 5317/4305 L11: Storage Allocation25 Example (cont.) Then we forward the pointer 5 of element 54

CSE 5317/4305 L11: Storage Allocation26 Example (cont.) The pointer 10 of element 56 has already been forwarded

CSE 5317/4305 L11: Storage Allocation27 Cheney’s Copying Collector It is good –Very simple allocation algorithm –No need for stack (since is not recursive) –Its run time depends on the number of live objects, not on the heap size –No fragmentation; compact memory –Allows incremental (concurrent) garbage collection It is bad –Needs double the amount of memory –Needs to recognize pointers to heap

CSE 5317/4305 L11: Storage Allocation28 Baker’s Concurrent GC Based on the copying garbage collector Does GC incrementally Avoids long pauses during GC Both from-space and to-space are used during program execution On a pointer dereference: if the pointer points to the from-space, forward the pointer (copy the object to the to-space) On GC: forward roots only and swap the names of the two spaces

CSE 5317/4305 L11: Storage Allocation29 Generational GC Observations: –If an object has survived a GC, it is likely to remain reachable for longer time –New objects are more likely to become garbage than older objects –Typically, <10% of new objects are live at GC GC should not waste time working on older objects Generational GC: assign objects to different generations G 0, G 1, G 2, … –G 0 : newest objects –G i is garbage collected more often than G i+1 –After GC, G i becomes G i+1 and we create a new generation G 0 Special case: two generations –New objects –Tenured objects