Memory Management I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt 15-213 Introduction to Computer Systems.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
The Linux Kernel: Memory Management
File Systems.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
1 Dynamic Memory Allocation: Basic Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaro.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Fixed/Variable Partitioning
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management.
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.
Dynamic memory allocation and fragmentation Seminar on Network and Operating Systems Group II.
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I Nov 5, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt “The course that gives.
1 Optimizing Malloc and Free Professor Jennifer Rexford
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Memory Allocation CS Introduction to Operating Systems.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
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.
1 Memory Management Requirements of memory management system to provide the memory space to enable several processes to execute concurrently to provide.
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.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
Writing You Own malloc() March 29, 2003 Topics Explicit Allocation Data structures Mechanisms Policies class19.ppt “The course that gives CMU its.
Memory Management Problem: Records (of various lengths) need to be stored. Model: A big array of space to store them, managed by a memory manager. Like.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Memory Management -Memory allocation -Garbage collection.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
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)
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
Dynamic Memory Allocation April 9, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies Reading: 10.9 Problems: and class21.ppt.
University of Washington Today More memory allocation!
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts Instructors: Adapted from CMU course
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
University of Washington Implementation Issues How do we know how much memory to free given just a pointer? How do we keep track of the free blocks? How.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Chapter 17 Free-Space Management
Instructor: Phil Gibbons
Memory Management Memory Areas and their use Memory Manager Tasks:
CS 3214 Introduction to Computer Systems
Dynamic Memory Allocation I
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I October 28, 2003
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
The Hardware/Software Interface CSE351 Winter 2013
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
CS Introduction to Operating Systems
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
User-Level Dynamic Memory Allocation: Malloc and Free
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
Dynamic Memory Allocation
Memory Management Overview
Dynamic Memory Allocation I
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Optimizing Dynamic Memory Management
Operating System Chapter 7. Memory Management
Dynamic Memory Allocation November 2, 2000
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Dynamic Memory Allocation I
Memory Management Memory Areas and their use Memory Manager Tasks:
Presentation transcript:

Memory Management I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt Introduction to Computer Systems

CS 213 F’98– 2 – class14.ppt Harsh Reality #3 Memory Matters Memory is not unbounded It must be allocated and managed Many applications are memory dominated –Especially those based on complex, graph algorithms Memory referencing bugs especially pernicious Effects are distant in both time and space Memory performance is not uniform Cache and virtual memory effects can greatly affect program performance Adapting program to characteristics of memory system can lead to major speed improvements

CS 213 F’98– 3 – class14.ppt Dynamic Storage Allocation Application Requests and frees contiguous blocks of memory Allocator (e.g., Unix malloc package) Provides an abstraction of memory as a set of blocks Doles out free memory blocks to application Keeps track of free and allocated blocks Heap memory region starting after bss segment Application Dynamic Storage Allocator Heap Memory

CS 213 F’98– 4 – class14.ppt Process memory image (Alpha) Reserved for kernel Reserved for shared libraries and dynamic loader Available for heap Heap (via malloc()) Grows up Bss segment Data segment Text segment Stack Not accessible Available for stack Grows down to zero Not accessible by convention (64KB) $gp $sp 0x x ffff 0x x fff ffff 0x x ff 7fff ffff 0x ff x ff ffff ffff 0x xffff fbff ffff ffff 0xffff fc xffff ffff ffff ffff

CS 213 F’98– 5 – class14.ppt Malloc package void *malloc(int size) if successful: –returns 8-byte aligned pointer to memory block of at least size bytes –is size=0, returns NULL if unsuccessful: –returns NULL void free(void *p) returns block pointed at by p to pool of available memory p must come from a previous call to malloc().

CS 213 F’98– 6 – class14.ppt Definitions and assumptions Allocated block (4 words) Free block (3 words) Relative Address (word address) Heap Memory (fixed size)... Program Primitives: Allocation request: Ai(n) –Allocate n words and call it block i –Example: A7(128) Free request: Fj –Free block j –Example: F7

CS 213 F’98– 7 – class14.ppt Allocation example A1(4) A2(5) A3(6) F2 A4(2)

CS 213 F’98– 8 – class14.ppt Constraints Applications: Can issue arbitrary sequence of allocation and free requests Free requests must correspond to an allocated block Allocators Can’t control number or size of allocated blocks Must respond immediately to all allocation requests –i.e., can’t reorder or buffer requests Must allocate blocks from free memory –i.e., can only place allocated blocks in free memory Must align blocks so they satisfy all alignment requirements –usually 8 byte alignment Can only manipulate and modify free memory Can’t move the allocated blocks once they are allocated –i.e., compaction is not allowed

CS 213 F’98– 9 – class14.ppt Fragmentation A1(4) A2(5) A3(6) F2 A4(6) oops!

CS 213 F’98– 10 – class14.ppt Fragmentation (cont) Def: (external) fragmentation is the inability to reuse free memory. possible because applications can free blocks in any order, potentially creating holes. Minimizing fragmentation is the fundamental problem of dynamic resource allocation... Unfortunately, there is no good operational definition. Function of Number and sizes of holes, –Placement of allocated blocks, –Past program behavior (pattern of allocates and frees) Future program behavior.

CS 213 F’98– 11 – class14.ppt Fragmentation (cont) Which heaps have a fragmentation problem? It depends... Qualitatively, C has fewer and bigger holes. But fragmentation occurs only if program needs a large block. Still, C is probably less likely to encounter problems. Definitive answer requires a model of program execution. C A B

CS 213 F’98– 12 – class14.ppt Fragmentation (cont) The policy for placing allocated blocks has a big impact on fragmentation A1(1) “First Fit” A2(2) A3(4) Oops!

CS 213 F’98– 13 – class14.ppt Fragmentation (cont) A1(1) “Best Fit” A2(2) A3(4) But “best fit” doesn’t always work best either

CS 213 F’98– 14 – class14.ppt Fragmentation (cont) A1(1) “Best Fit” A2(2) A3(2) A4(2) oops!

CS 213 F’98– 15 – class14.ppt Fragmentation (cont) A1(1) “First Fit” A2(2) A3(2) A4(2)

CS 213 F’98– 16 – class14.ppt Splitting A1(1) (1) Find a free block that is big enough (2) Split the block into two free blocks (3) Allocate the first block

CS 213 F’98– 17 – class14.ppt Coalescing 2 F2 (1) free the block (2) merge any adjacent free blocks into a single free block Crucial operation for any dynamic storage allocator Can be: immediate (performed at every free request) deferred (performed every k free requests or when necessary)

CS 213 F’98– 18 – class14.ppt Organizing the set of free blocks Some data structure needed to organize the search of the free blocks. Efficient implementations use the free blocks themselves to hold the necessary link fields. disadvantage: every allocated block must be large enough to hold the link fields (since the block could later be freed) imposes a minimum block size could result in wasted space (internal fragmentation) Common approach: list of free blocks embedded in an array of allocated blocks.

CS 213 F’98– 19 – class14.ppt Organizing the set of free blocks address ordering – no memory overhead doubly linked list of free blocks –simple, popular, reasonable memory overhead –might not scale to large sets of free blocks tree structures –more scalable –less memory efficient than lists segregated free lists –different free lists for different size classes of free blocks. –internal fragmentation

CS 213 F’98– 20 – class14.ppt Placement policies When a block is allocated, we must search the free list for a free block that is large enough to satisfy the request (feasible free block). Placement policy determines which feasible free block to choose. A1(1) Each of these free blocks is feasible. Where do we place the allocated block?

CS 213 F’98– 21 – class14.ppt Placement policies (cont) first fit –search list from beginning, choose first free block that fits. –simple and popular –can increase search time, because “splinters” can accumulate near the front of the list. –simplicity lends itself to tight inner loop –might not scale well for large free lists. A1(1) First fit chooses this block Search starts here

CS 213 F’98– 22 – class14.ppt Placement policies (cont) best fit –choose free block that fits the best –motivation is to try to keep fragments (what’s left over after splitting) as small as possible –can backfire if blocks almost fit, but not quite. A1(1) Best fit chooses this block Search starts here

CS 213 F’98– 23 – class14.ppt Placement policies (cont) next fit [Knuth] –like first fit, but instead of starting each search at the beginning... –use a roving pointer to remember where last search was satisfied. –begin next search at this point. –motivation is to decrease average search time. –potential disadvantage: can scatter blocks from one program throughout memory, adversely affecting locality. A1(1) Roving pointer Next fit chooses this free block

CS 213 F’98– 24 – class14.ppt Implementation Issues a 1 word size Format of allocated and free blocks data a = 1: allocated block a = 0: free block size: block size data: application data (allocated blocks only) The simplest allocator allocate time: linear in total number of blocks free time: linear in total number of blocks min block size: two words

CS 213 F’98– 25 – class14.ppt Implementation issues A simple space optimization: exploit unused lower order size bits block size always a multiple of the wordsize reduces minimum block size from 2 words to 1 word size 1 word Format of allocated and free blocks data a = 1: allocated block a = 0: free block size: block size data: application data (allocated blocks only) a

CS 213 F’98– 26 – class14.ppt Implementation issues Boundary tags [Knuth73] replicate size/allocated word at bottom of free blocks allocate time: linear in total number of blocks free time: constant time minimum block size: 2 words size 1 word Format of allocated and free blocks data a = 1: allocated block a = 0: free block size: block size data: application data (allocated blocks only) a sizea boundary tag

CS 213 F’98– 27 – class14.ppt Constant time coalescing allocated free allocated free block being freed Case 1Case 2Case 3Case 4

CS 213 F’98– 28 – class14.ppt Food for thought How can we use a list of free block to reduce the search time to linear in the number of free blocks? Can we avoid having two conditionals in the inner loop of the free block list traversal one to check size one to check that entire list has been searched Can we implement a free list algorithm with constant time coalescing and a minimum block size of three words instead of four words?

CS 213 F’98– 29 – class14.ppt Internal fragmentation Internal fragmentation is wasted space inside allocated blocks: minimum block size larger than requested amount –e.g., due to minimum free block size, free list overhead policy decision not to split blocks –e.g., allocating from segregated free lists (see [Wilson85]) Much easier to define and measure than external fragmentation. Source of interesting computer science forensic techniques in the context of disk blocks contents of “slack” at the end of the last sector of a file contain directory entries. provide a snapshop of the system that copied the file.

CS 213 F’98– 30 – class14.ppt For more information D. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973 the classic reference on dynamic storage allocation Wilson et al, “Dynamic Storage Allocation: A Survey and Critical Review”, Proc Int’l Workshop on Memory Management, Kinross, Scotland, Sept, comprehensive survey /afs/cs/academic/class/15-213/doc/dsa.ps