11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.

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
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
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.
Chapter FourteenModern Programming Languages1 Memory Management.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
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
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 Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run time vs. Compile time
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Garbage collection (& Midterm Topics) David Walker COS 320.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
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.
Memory Allocation CS Introduction to Operating Systems.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
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.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Storage Management - Chap 10 MANAGING A STORAGE HIERARCHY on-chip --> main memory --> 750ps - 8ns ns. 128kb - 16mb 2gb -1 tb. RATIO 1 10 hard disk.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Chapter FourteenModern Programming Languages1 Memory Management.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Memory Management -Memory allocation -Garbage collection.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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)
Dynamic Memory Allocation II
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
CSC 533: Programming Languages Spring 2016
Storage Allocation Mechanisms
Section 10: Memory Allocation Topics
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Memory Management © 2004, D. J. Foreman.
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
The Hardware/Software Interface CSE351 Winter 2013
Optimizing Malloc and Free
Heap storage & Garbage collection
Presentation made by Steponas Šipaila
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CSC 533: Programming Languages Spring 2019
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation records – Objects – Explicit allocations: new, malloc, etc. – Implicit allocations: strings, file buffers, arrays with dynamically varying size, etc. It is a 800-pages topic, but here we want to do it in one/two class!

11/26/2015IT 3272 Memory Model n For now, assume that the OS grants each running program one or more fixed-size regions of memory for dynamic allocation n We will model these regions as an array – Our textbook shows an examples of memory management code in Java Yes! It’s OS’s job!!

11/26/2015IT 3273 Stacks Of Activation Records n Activation records must be allocated dynamically n Allocate on call and de-allocate on return n Stack of activation records: push and pop A simple memory management problem

11/26/2015IT 3274 A Stack Illustration An empty stack of 8 words. grow reg.

11/26/2015IT 3275 The manager program calls m.push(3). Some program P needs 3 words P gets this pointer

11/26/2015IT 3276 Some other program Q needs 2 words The manager program calls m.push(2). P gets this pointer Q gets this pointer wasted

11/26/2015IT 3277 Heap n Stack is easy to do, but Allocations and deallocations may come in any order? n A heap is a pool of blocks of memory At the beginning there is only one block (but not always the case) There are many approaches for doing this…

11/26/2015IT 3278 How to deal blocks to the requests? A pool of free blocks are linked, initially containing one big free block, then fragmented into small blocks – Search the free list for first adequate block – If there is extra space in the block, return the unused portion to front of the free list – Allocate requested portion (at the lower end) The easiest approach: First Fit for request To free a block, just add to the front of the free list

11/26/2015IT 3279 Heap implementation in an array Size of this block Pointer to next block, -1 means no next block.

11/26/2015IT Size of this free block Size of this block for P Some program P requests 4 words P gets this pointer

11/26/2015IT Size of this block Q gets this pointer Some program Q requests 2 words

11/26/2015IT P is done

11/26/2015IT Some program R requests 1 word R gets this pointer

11/26/2015IT A Problem n Consider this sequence: n The manager needs to coalesce adjacent free blocks. 1.p1=m.allocate(4); 2.p2=m.allocate(4); 3.m.deallocate(p1); 4.m.deallocate(p2); 5.p3=m.allocate(7); p1 p2 5 5 p2  top How?

11/26/2015IT Improvement strategy n Quick lists, a separate list, same sized blocks n Delayed coalescing Keep separate free lists for popular (small) block sizes

11/26/2015IT p1=m.allocate(4); p2=m.allocate(1); m.deallocate(p1); p3=m.allocate(5); The final allocation will fail because of fragmentation. Another problem: Fragmentation DeFragmentation

11/26/2015IT Heap Mechanisms (to manage heaps.) n Three major issues: – Placement—where to allocate a block – Splitting—when and how to split large blocks – Coalescing—when and how to recombine Many variety and different refinements

11/26/2015IT Placement: Where to allocate a block n (FF) First Fit from a stack (FIFO) of free list n (BF) Best Fit … n (NF) Next Fit … Some mechanisms use a more scalable data structure like a Balanced Binary Tree

11/26/2015IT Splitting: When and how to split large blocks n Split to requested size n Sometimes, less splitting gives better results e.g., allocate more than requested or rounding up allocation size to some multiple, for example 2 n

11/26/2015IT Coalescing: When and how to recombine adjacent free blocks n Several varieties: – No coalescing – Eager coalescing – Delayed coalescing

11/26/2015IT Current Heap Links n Some systems track current heap links n A current heap link is a memory address (location in the heap) where the running program may use a = new X(i); b = new X(j); a = b; abab What if

11/26/2015IT Tracing Current Heap Links IntList a = new IntList(null); int b = 2; int c = 1; a = a.cons(b); a = a.cons(c); Where are the current heap links in this picture?

11/26/2015IT To Find Current Heap Links n Start with the root set: memory locations outside of the heap with links into the heap We have to check: – Active activation records (if on the stack) – Static variables, etc. n For each memory location in the set, look at the allocated block it points to, and add all the memory locations in that block n Repeat until no new locations are found {a b c } They are not in the active activation record.

11/26/2015IT Possible Errors In Current Heap Links 1. Exclusion errors: a memory location that actually is a current heap link is left out. (This type of error is not allowed) Current Activation Record p z:200 r[3] Static variables s x:123 y: a b c d Heap Current heap links p, r, a

11/26/2015IT Possible Errors In Current Heap Links 2. Unused inclusion errors: a memory location is included, but the program never actually uses the value stored there Current Activation Record p z:200 r[3] Static variables s x:123 y: a b c d Heap Current heap links s, p, r, a, c, d How do I know this is the case?

11/26/2015IT Possible Errors In Current Heap Links 3. Used inclusion errors (mistaken): a memory location is included, but the program uses the value stored there as something other than a heap address (e.g. int) Current Activation Record p z:200 r[3] Static variables s x:123 y: a b c d Heap Current heap links s, p, r, a, c, y, z How do I know this is the case?

11/26/2015IT Errors Are Unavoidable n For heap manager purposes, exclusion errors are unacceptable Therefore, we must include a location if it might be used as a heap link, and n This makes unused inclusion errors unavoidable n Depending on the language, used inclusions errors (mistaken) may also be unavoidable

11/26/2015IT An application for current heap links: Heap Compaction/Defragmentation n Manager can re-allocate blocks: – Copy the block to a new location – Update all links to (or into) that block n So it can compact the heap, moving all allocated blocks to one end, leaving one big free block and no fragmentation Q P P Q S R T R S T

11/26/2015IT n There are so many errors caused by improper de- allocation (e.g., Bad or without class destructors in C/C++) n It is a burden on the programmer … Therefore, let the language system do the job automatically --- Garbage Collection

11/26/2015IT Garbage Collection 1. Mark and sweep 2. Copying 3. Reference counting Three Major Approaches

11/26/2015IT Mark And Sweep uses current heap links in a two-stage process: – Mark: find the live heap links and mark all the heap blocks pointed by them – Sweep: make a pass over the heap and return unmarked blocks to the free pool - both kinds of inclusion errors are tolerated, - blocks are not moved, fragmentation may remain

11/26/2015 IT 327 Mark And Sweep P S R T P S R T Mark: live heap links (in red) Sweep: return unmarked blocks to the free pool blocks are not moved / fragmentations may remain Free blocks 32

11/26/2015IT Copying Collection n divide memory in half, and uses only one half at a time n When one half becomes full, find live heap links, and copy live blocks to the other half n Fragmentation is eliminated n Moves blocks Cannot tolerate used inclusion errors. Why? Because that used variable will be mistaken as a link and hence modified

11/26/2015IT Q P P Q S R T R S T Copying Collection  Moves blocks: cannot tolerate used inclusion errors (mistake) X=700 X=100

11/26/2015IT Reference Counting n Each block has a counter of heap links to it n Incremented when a heap link is copied, decremented when a heap link is discarded n When counter goes to zero, block is garbage and can be freed n Does not use current heap links

11/26/2015IT Reference Counting

11/26/2015IT Reference Counting Problem Reference counters are not zero But all blocks are garbage. Garbage cycles can’t be identified

11/26/2015IT Reference Counting n Problem 1: cycles of garbage n Problem 2: overhead of updating reference counters is high n One advantage: more uniform. I.e., cost is incremental, no big pause while collecting

11/26/2015IT Garbage Collecting Refinements n Generational collectors – Divide block into generations according to age – Garbage collect in younger generations more often (using previous methods) n Incremental collectors – Collect garbage a little at a time – Avoid the uneven performance like mark-and- sweep and copying collectors

11/26/2015IT Languages with Garbage Collectors n Required: Java, ML n encouraged: Ada n difficult to have: C, C++ – But not impossible, even for C and C++ – There are libraries that replace the usual malloc / free with a garbage-collecting manager

11/26/2015IT Conclusion n Memory management is an important hidden player in language systems n Performance and reliability are critical n Different techniques are difficult to compare, since every run of every program makes different memory demands n Still an active area of language systems research and experimentation