Memory Management Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
Memory Management Chapter FourteenModern Programming Languages, 2nd ed.1.
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
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
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.
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,
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
User-Level Memory Management in Linux Programming
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
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.
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
Garbage Collection Mooly Sagiv html://
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Management Chapter 5 Mooly Sagiv
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Garbage Collection Mooly Sagiv
CS61C L07 More Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
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.
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.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Go’LegOS Alternative Memory Management Schemes for LegOS Project Hao Ji Yanhao Zhu Gowri Mereddy.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
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.
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)
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
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.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Garbage Collection What is garbage and how can we deal with it?
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Concepts of programming languages
Strategies for automatic memory management
Memory Allocation CS 217.
Chapter 12 Memory Management
CS703 - Advanced Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

Memory Management Professor Yihjia Tsai Tamkang University

Overview: memory management explicit deallocation malloc() + free() implicit deallocation: garbage collection reference counting mark & scan two-space copying

Memory management What has a compiler to do with memory management? compiler uses heap-allocated data structures modern languages have automatic data (de)allocation garbage collection part of runtime support system compiler usually assists in identifying pointers

Data allocation with explicit deallocation malloc() find free block of requested size mark it in use return a pointer to it. free() mark the block as not in use. #include void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size);

Heap layout SIZESIZE S 0 SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... lowhigh marked free marked in use block chunk in use free pointer to user data

Free() PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Chunk pointer.free TO True; SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 in use SIZESIZE S 1 free

Malloc() SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Pointer TO Free block of size (Block size); IF pointer /= NULL: RETURN pointer; Coalesce free chunks (); RETURN Free block of size (Block size); SIZESIZE S 0 in use SIZESIZE S 1 free

walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer SIZESIZE S 0 in use SIZESIZE S 1 free Free block of size (request) SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 block pointer

walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer SIZESIZE S 0 in use SIZESIZE S 1 free Free block of size (request) walk chunks from low to high check if chunk is free AND large enough if so, mark chunk in use AND return block pointer optimization: split chunk to free unused part SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... free SIZESIZE S 0 SIZESIZE S 1 block pointer

Free block of size FUNCTION Free block of size (Block size) RETURNS a generic pointer: SET Chunk ptr TO Heap low; SET Request TO Block size + Admin size; WHILE Chunk ptr < Heap high: IF Chunk ptr.free AND Chunk ptr.size >= Request: Split chunk (Chunk ptr, Request) SET Chunk ptr.free TO False; RETURN Chunk ptr + Admin size; SET Chunk ptr TO Chunk ptr + Chunk ptr.size; RETURN NULL;

next SIZESIZE S 0 in use SIZESIZE S 1 free Coalesce free chunks () walk chunks from low to high check if chunk is free if so, coalesce all subsequent free chunks SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... SIZESIZE S 0 SIZESIZE S 1 next

Coalesce free chunks PROCEDURE Coalesce free chunks (): SET Chunk ptr TO Heap low; WHILE Chunk ptr < Heap high: IF Chunk ptr.free: SET Next TO Chunk ptr + Chunk ptr.size; WHILE Next < Heap high AND Next.free: SET Next TO Next + Next.size; SET Chunk ptr.size TO Next - Chunk ptr; SET Chunk ptr TO Chunk ptr + Chunk ptr.size;

Optimizations free: poor performance (linear search) malloc: irregular performance (coalesce phase) solutions: free lists indexed by size coalesce at free() SIZESIZE S 0 in use SIZESIZE S 1 free SIZESIZE S 1 SIZESIZE S 0 SIZESIZE S 1... SIZESIZE S 0 SIZESIZE S 1 2 log(size)3456 free list use first field as next ptr

Malloc() with free lists FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Chunk size TO Block size + Admin size; SET Index TO 2 log(Chunk size); IF Index < 3: SET Index TO 3; IF Index <= 10 AND Free list[Index] /= NULL: SET Pointer TO Free list[Index]; SET Free list[Index].next TO Pointer.next; RETURN Pointer + Admin size; RETURN Free block of size (Block size);

Exercise (5 min.) give the pseudo code for free() when using free lists indexed by size.

Answers

PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Index TO 2 log(Chunk pointer.size); IF Index <= 10: SET Chunk pointer.next TO Free list[Index]; SET Free list[Index] TO Chunk pointer; ELSE SET Chunk pointer.free TO True; // Coalesce subsequent free chunks

Garbage collection memory allocation is explicit (new) memory deallocation is implicit garbage set: all chunks that will no longer be used by the program chunks without incoming pointers chunks that are unreachable from non-heap data

Example A C D E F B heap root set

Garbage A C D E B heap root set B B E E F

Cyclic garbage C E F B heap root set D A garbage? “no-pointers”: NO “not-reachable”: YES

Compiler assistance: identifying pointers pointers inside chunks user-defined data structures compiler: generate self-descriptive chunks pointers located outside the heap (root set) global data + stack compiler: generate activation record descriptions

Self-descriptive chunks bitmap per data type problem: overhead per chunk / interpretation compiler-generated routine per data type calls GC for each pointer problem: recursion organize data type to start off with n pointers solution: n can be squeezed into chunk admin

Reference counting record #pointers to each chunk reclaim when reference count drops to zero heap root set AD BE FC

Maintaining reference counts VAR p, q : pointer;... p := q; PROCEDURE Free recursively (Pointer): FOR each field f i of record Pointer: IF Points into the heap (f i ): Decrement f i.ref count; IF f i.ref count = 0: Free recursively (f i ); Free chunk (Pointer); source IF Points into the heap (q): Increment q.ref count; IF Points into the heap (p): Decrement p.ref count; IF p.ref count = 0: Free recursively (p); SET p TO q; target pointer assignment:

Mark & scan A C D E F B heap root set mark all reachable chunks scan heap for unmarked chunks that can be freed B E

Mark & scan PROCEDURE Mark (Pointer): IF NOT Points into the heap (Pointer): RETURN; SET Pointer.marked TO True; FOR each field f i of record Pointer: Mark (f i ); PROCEDURE Scan (): SET Chunk ptr TO Heap low; WHILE Chunk ptr < Heap high: IF Chunk ptr.marked: SET Chunk ptr.marked TO False; ELSE SET Chunk ptr.free TO True; SET Chunk ptr TO Chunk ptr + Chunk ptr.size;

Advanced marking problem: mark() is recursive solution: embed stack in the chunks each chunk records: a count denoting which child pointer is next a pointer to the parent node

Advanced marking S ptrptr ptrptr ptrptr S ptrptr ptrptr ptrptr free bit mark bit pointer cnt size to parent 0 0 S ptrptr ptrptr ptrptr

Advanced marking: pointer reversal avoid additional parent pointer use the n-th child pointer when visiting child n S ptrptr ptrptr ptrptr S ptrptr ptrptr ptrptr to parent

Two-space copying most chunks have a short live time memory fragmentation must be addressed partition heap in two spaces copy all reachable chunks to consecutive locations fromto

Two-space copying most chunks have a short live time memory fragmentation must be addressed partition heap in two spaces copy all reachable chunks to consecutive locations fromtofrom to

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C D E F B from to A C A scan

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E F B from to A C scan A D

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E F B from to A C scan A D

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C scan A D F

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space C E B from to A C A D F scan

Copying to to-space copy root set leave forwarding pointers scan to-space for reachable cells in from-space from to C A D F scan

Summary Memory management explicit deallocation malloc() + free() implicit deallocation: garbage collection reference counting mark & scan two-space copying