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.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation II
Advertisements

1. Dynamic Memory Allocation. Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data.
1 Binghamton University Dynamic Memory Allocation: Advanced Concepts CS220: Computer Systems II.
Dynamic Memory Allocation Topics Simple explicit allocators Data structures Mechanisms Policies.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
University of Washington Today Dynamic memory allocation  Size of data structures may only be known at run time  Need to allocate space on the heap 
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
Carnegie Mellon 1 Dynamic Memory Allocation: Advanced Concepts : Introduction to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 31 st Instructors: Gregory Kesden and Markus Püschel.
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.
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.
Dynamic Memory Allocation I May 21, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies.
1 Optimizing Malloc and Free Professor Jennifer Rexford
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
Explicit Free Lists Use data space for link pointers –Typically doubly linked –Still need boundary tags for coalescing –It is important to realize that.
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.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists Garbage collection.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Monday March 30th,
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
Today Moving on to… Memory allocation.
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!
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
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)
1 Dynamic Memory Allocation (II) Implementation. 2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading:
University of Washington Today More memory allocation!
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Recitation 11: Monday, Nov 3, 2014 SHAILIN DESAI SECTION L 1.
Dynamic Memory Allocation Alan L. Cox Some slides adapted from CMU slides.
Memory Management I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt Introduction to Computer Systems.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Advanced Concepts :
Instructor: Phil Gibbons
Dynamic Memory Allocation II
Section 10: Memory Allocation Topics
CS 3214 Introduction to Computer Systems
Dynamic Memory Allocation I
Dynamic Memory Allocation
Instructors: Roger Dannenberg and Greg Ganger
Instructor: Haryadi Gunawi
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Dynamic Memory Allocation I October 28, 2003
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
Memory Allocation I CSE 351 Spring 2017
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
Memory Allocation I CSE 351 Spring 2017
User-Level Dynamic Memory Allocation: Malloc and Free
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
Dynamic Memory Allocation
Dynamic Memory Allocation I
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Memory Allocation II CSE 351 Autumn 2017
Memory Allocation II CSE 351 Winter 2018
Dynamic Memory Allocation November 2, 2000
Memory Allocation II CSE 351 Winter 2018
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Dynamic Memory Allocation I
Instructors: Majd Sakr and Khaled Harras
CS703 - Advanced Operating Systems
Presentation transcript:

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 do we pick a block to use for allocation (when many might fit)? What do we do with the extra space when allocating a structure that is smaller than the free block it is placed in? How do we reinsert freed block into the heap? 1

University of Washington Knowing How Much to Free 2

University of Washington Knowing How Much to Free Standard method  Keep the length of a block in the word preceding the block  This word is often called the header field or header  Requires an extra word for every allocated block free(p0) p0 = malloc(16) 3 p0 block sizedata 20

University of Washington Keeping Track of Free Blocks 4

University of Washington Keeping Track of Free Blocks Method 1: Implicit list using length—links all blocks Method 2: Explicit list among the free blocks using pointers Method 3: Segregated free list  Different free lists for different size classes Method 4: Blocks sorted by size  Can use a balanced binary tree (e.g. red-black tree) with pointers within each free block, and the length used as a key

University of Washington Implicit Free Lists For each block we need: size, is-allocated?  Could store this information in two words: wasteful! Standard trick  If blocks are aligned, some low-order size bits are always 0  Instead of storing an always-0 bit, use it as a allocated/free flag  When reading size, must remember to mask out this bit size 1 word Format of allocated and free blocks payload a = 1: allocated block a = 0: free block size: block size payload: application data (allocated blocks only) a optional padding 6 e.g. with 8-byte alignment, sizes look like: …

University of Washington Implicit Free List Example (32-bit) 8-byte alignment  May require initial unused word  Causes some internal fragmentation Special one-word marker (0|1) marks end of list  zero size is distinguishable from all real sizes 8|016|132|016|10|1 Free word Allocated word Allocated word unused Start of heap 8 bytes = 2 word alignment Sequence of blocks in heap (size|allocated): 8|0, 16|1, 32|0, 16|1 7

University of Washington Implicit List: Finding a Free Block First fit:  Search list from beginning, choose first free block that fits:  Can take time linear in total number of blocks (allocated and free)  In practice it can cause “splinters” at beginning of list Next fit:  Like first-fit, but search list starting where previous search finished  Should often be faster than first-fit: avoids re-scanning unhelpful blocks  Some research suggests that fragmentation is worse Best fit:  Search the list, choose the best free block: fits, with fewest bytes left over  Keeps fragments small—usually helps fragmentation  Will typically run slower than first-fit p = heap_start; while ((p < end) && // not past end ((*p & 1) || // already allocated (*p <= len))) { // too small p = p + (*p & -2); // go to next block (UNSCALED +) } // p points to selected block or end 8 *p gets the block header *p & 1 extracts the allocated bit *p & -2 masks the allocated bit, gets just the size

University of Washington 8 Implicit List: Allocating in Free Block Allocating in a free block: splitting  Since allocated space might be smaller than free space, we might want to split the block void split(ptr b, int bytes) { // bytes = desired block size int newsize = ((bytes + 7) >> 3) << 3;// round up to multiple of 8 int oldsize = *b; // why not mask out low bit? *b = newsize; // initially unallocated if (newsize < oldsize) *(b+newsize) = oldsize - newsize; // set length in remaining } // part of block (UNSCALED +) b malloc(12)  split(b, 16) 9 assume ptr points to word and has unscaled pointer arithmetic

University of Washington Implicit List: Freeing a Block Simplest implementation:  Need only clear the “allocated” flag void free(ptr p) { ptr b = p – WORD; *b = *b & -2 }  But can lead to “false fragmentation” There is enough free space, but the allocator won’t be able to find it p malloc(20) Oops! 10 free(p)

University of Washington Implicit List: Coalescing Join (coalesce) with next/previous blocks, if they are free  Coalescing with next block  But how do we coalesce with the previous block? void free(ptr p) { // p points to data ptr b = p – WORD; // b points to block *b = *b & -2; // clear allocated bit ptr next = b + *b; // find next block (UNSCALED +) if ((*next & 1) == 0) *b = *b + *next; // add to this block if } // not allocated free(p) logically gone 11 p

University of Washington Implicit List: Bidirectional Coalescing Boundary tags [Knuth73]  Replicate size/allocated word at “bottom” (end) of free blocks  Allows us to traverse the “list” backwards, but requires extra space  Important and general technique! size Format of allocated and free blocks payload and padding a = 1: allocated block a = 0: free block size: total block size payload: application data (allocated blocks only) a sizea Boundary tag (footer) Header 12

University of Washington Constant Time Coalescing allocated free allocated free block being freed Case 1Case 2Case 3Case 4 13

University of Washington Constant Time Coalescing m11 1 n1 n1 m21 1 m11 1 n0 n0 m m11 1 n+m20 0 m11 1 n1 n1 m20 0 m10 0 n1 n1 m21 1 n+m10 0 m21 1 m10 0 n1 n1 m20 0 n+m1+m20 0

University of Washington Implicit Free Lists: Summary Implementation: very simple Allocate cost:  linear time (in total number of heap blocks) worst case Free cost:  constant time worst case  even with coalescing Memory utilization:  will depend on placement policy  First-fit, next-fit or best-fit Not used in practice for malloc()/free() because of linear-time allocation  used in some special purpose applications The concepts of splitting and boundary tag coalescing are general to all allocators 15

University of Washington Keeping Track of Free Blocks Method 1: Implicit free list using length—links all blocks Method 2: Explicit free list among the free blocks using pointers Method 3: Segregated free list  Different free lists for different size classes Method 4: Blocks sorted by size  Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key

University of Washington Explicit Free Lists Maintain list(s) of free blocks, rather than implicit list of all blocks  The “next” free block could be anywhere in the heap  So we need to store forward/back pointers, not just sizes  Luckily we track only free blocks, so we can use payload area for pointers  Still need boundary tags for coalescing sizea a next prev Free block: 17 size payload and padding a sizea Allocated block: (same as implicit free list)

University of Washington Explicit Free Lists Logically (doubly-linked lists): Physically? ABC 18

University of Washington Explicit Free Lists Logically (doubly-linked lists): Physically: blocks can be in any order ABC Forward (next) links Back (prev) links A B C 19

University of Washington Allocating From Explicit Free Lists Before conceptual graphic 20

University of Washington Allocating From Explicit Free Lists Before After = malloc(…) (with splitting) conceptual graphic 21

University of Washington Freeing With Explicit Free Lists Insertion policy: Where in the free list do you put a newly freed block? 22

University of Washington Freeing With Explicit Free Lists Insertion policy: Where in the free list do you put a newly freed block?  LIFO (last-in-first-out) policy  Insert freed block at the beginning of the free list  Pro: simple and constant time  Con: studies suggest fragmentation is worse than address ordered  Address-ordered policy  Insert freed blocks so that free list blocks are always in address order: addr(prev) < addr(curr) < addr(next)  Con: requires linear-time search when blocks are freed  Pro: studies suggest fragmentation is lower than LIFO Cache effects? 23

University of Washington Freeing With a LIFO Policy (Case 1) Insert the freed block at the root of the list free( ) Root Before After conceptual graphic 24

University of Washington Freeing With a LIFO Policy (Case 2) Splice out predecessor block, coalesce both memory blocks, and insert the new block at the root of the list free( ) Root Before After conceptual graphic 25

University of Washington Freeing With a LIFO Policy (Case 3) Splice out successor block, coalesce both memory blocks and insert the new block at the root of the list free( ) Root Before After conceptual graphic 26

University of Washington Freeing With a LIFO Policy (Case 4) Splice out predecessor and successor blocks, coalesce all 3 memory blocks and insert the new block at the root of the list free( ) Root Before After conceptual graphic 27

University of Washington Do we always need the boundary tag? Lab 5 suggests no… 28 sizea a next prev Free block: size payload and padding a sizea Allocated block:

University of Washington Explicit List Summary Comparison to implicit list:  Allocate is linear time in number of free blocks instead of all blocks  Much faster when most of the memory is full  Slightly more complicated allocate and free since needs to splice blocks in and out of the list  Some extra space for the links (2 extra words needed for each block)  Possibly increases minimum block size, leading to more internal fragmentation Most common use of explicit lists is in conjunction with segregated free lists  Keep multiple linked lists of different size classes, or possibly for different types of objects 29

University of Washington Keeping Track of Free Blocks Method 1: Implicit list using length—links all blocks Method 2: Explicit list among the free blocks using pointers Method 3: Segregated free list  Different free lists for different size classes Method 4: Blocks sorted by size  Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key

University of Washington Segregated List (Seglist) Allocators Each size class of blocks has its own free list Often have separate classes for each small size For larger sizes: One class for each two-power size inf 31 8

University of Washington Seglist Allocator Given an array of free lists, each one for some size class To allocate a block of size n:  Search appropriate free list for block of size m > n  If an appropriate block is found:  Split block and place fragment on appropriate list (optional)  If no block is found, try next larger class  Repeat until block is found If no block is found:  Request additional heap memory from OS (using sbrk() )  Allocate block of n bytes from this new memory  Place remainder as a single free block in largest size class 32

University of Washington Seglist Allocator To free a block:  Coalesce and place on appropriate list (optional) Advantages of seglist allocators  Higher throughput  log time for power-of-two size classes  Better memory utilization  First-fit search of segregated free list approximates a best-fit search of entire heap.  Extreme case: Giving each block its own size class is equivalent to best-fit. 33

University of Washington Summary of Key Allocator Policies Placement policy:  First-fit, next-fit, best-fit, etc.  Trades off lower throughput for less fragmentation  Observation: segregated free lists approximate a best fit placement policy without having to search entire free list Splitting policy:  When do we go ahead and split free blocks?  How much internal fragmentation are we willing to tolerate? Coalescing policy:  Immediate coalescing: coalesce each time free() is called  Deferred coalescing: try to improve performance of free() by deferring coalescing until needed. Examples:  Coalesce as you scan the free list for malloc()  Coalesce when the amount of external fragmentation reaches some threshold 34

University of Washington More Info on Allocators D. Knuth, “The Art of Computer Programming”, 2 nd 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  Available from CS:APP student site (csapp.cs.cmu.edu) 35

University of Washington Administrivia HW 3 solutions are posted  link on the HW 3 page Optional ungraded memory allocation HW-style problems  on lab 5 page  solutions available this Friday 36