Dynamic Memory Allocation IIApril 1, 2004Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and pitfallsclass22.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, S’04Keeping Track of Free Blocks Method 1: Implicit list using lengths -- links all blocks Method 2: Explicit list among the free blocks using pointers within the free blocks Method 3: Segregated free lists Different free lists for different size classes Method 4: Blocks sorted by size (not discussed) Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key5 4 265 4 26– 3 –15-213, S’04Explicit Free ListsUse data space for link pointers Typically doubly linked Still need boundary tags for coalescing It is important to realize that links are not necessarily in thesame order as the blocksA B C4 4 4 4 66 44 4 4Forward linksBack linksABC– 4 –15-213, S’04Allocating From Explicit Free Listsfree blockpred succfree blockpred succBefore:After:(with splitting)– 5 –15-213, S’04Freeing With Explicit Free ListsInsertion 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» i.e. addr(pred) < addr(curr) < addr(succ) Con: requires search Pro: studies suggest fragmentation is better than LIFO– 6 –15-213, S’04Freeing With a LIFO PolicyCase 1: a-a-a Insert self at beginning of free listCase 2: a-a-f Splice out next, coalesce self and next, and add to beginning of free listselfa ap sselfa fbefore:p sfaafter:rootx– 7 –15-213, S’04Freeing With a LIFO PolicyCase 1: a-a-a Insert self at beginning of free listCase 2: a-a-f Splice out next, coalesce self and next, and add to beginning of free listselfa ap sselfa fbefore:p sfaafter:rootx– 8 –15-213, S’04Freeing With a LIFO Policy (cont)Case 3: f-a-a Splice out prev, coalesce with self, and add to beginning of free listCase 4: f-a-f Splice out prev and next, coalesce with self, and add to beginning of listp sselff abefore:p sf aafter:p1 s1selff fbefore:fafter:p2 s2p1 s1 p2 s2– 9 –15-213, S’04Explicit List SummaryComparison to implicit list: Allocate is linear time in number of free blocks instead of total blocks -- much faster allocates 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)Main use of linked lists is in conjunction with segregated free lists Keep multiple linked lists of different size classes, or possibly for different types of objectsDoes this increase internal frag?– 10 –15-213, S’04Keeping Track of Free BlocksMethod 1: Implicit list using lengths -- links all blocksMethod 2: Explicit list among the free blocks using pointers within the free blocksMethod 3: Segregated free list Different free lists for different size classesMethod 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 key54 2654 26– 11 –15-213, S’04Segregated StorageEach size class has its own collection of blocks1-2345-89-16 Often have separate size class for every small size (2,3,4,…) For larger sizes typically have a size class for each power of 2– 12 –15-213, S’04Simple Segregated StorageSeparate heap and free list for each size classNo splittingTo allocate a block of size n: If free list for size n is not empty, allocate first block on list (note, list can be implicit or explicit) If free list is empty, get a new page create new free list from all blocks in page allocate first block on list Constant timeTo free a block: Add to free list If page is empty, return the page for use by another size (optional)Tradeoffs: Fast, but can fragment badly– 13 –15-213, S’04Segregated FitsArray of free lists, each one for some size classTo 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 foundTo free a block: Coalesce and place on appropriate list (optional)Tradeoffs Faster search than sequential fits (i.e., log time for power of two size classes) Controls fragmentation of simple segregated storage Coalescing can increase search times Deferred coalescing can help – 14 –15-213, S’04For More Info on AllocatorsD. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973 The classic reference on dynamic storage allocationWilson et al, “Dynamic Storage Allocation: A Survey and Critical Review”, Proc. 1995 Int’l Workshop on Memory Management, Kinross, Scotland, Sept, 1995. Comprehensive survey Available from CS:APP student site (csapp.cs.cmu.edu)– 15 –15-213, S’04Implicit Memory Management:Garbage CollectionGarbage collection: automatic reclamation of heap-allocated storage -- application never has to freeCommon in functional languages, scripting languages, and modern object oriented languages: Lisp, ML, Java, Perl, Mathematica, Variants (conservative garbage collectors) exist for C and C++ However, cannot necessarily collect all garbagevoid foo() {int *p = malloc(128);return; /* p block is now garbage */}– 16 –15-213, S’04Garbage CollectionHow does the memory manager know when memory can be freed? In general we cannot know what is going to be used in the future since it depends on conditionals But we can tell that certain blocks cannot be used if there are no pointers to themNeed to make certain assumptions about pointers Memory manager can distinguish pointers from non-pointers All pointers point to the start of a block Cannot hide pointers (e.g., by coercing them to an int, and then back again)– 17 –15-213, S’04Classical GC algorithmsMark and sweep collection (McCarthy, 1960) Does not move blocks (unless you also “compact”)Reference counting (Collins, 1960) Does not move blocks (not
View Full Document