Dynamic Memory Allocation IIMarch 27, 2008TopicsExplicit doubly-linked free listsSegregated free listsGarbage collectionReview of pointersMemory-related perils and pitfallsclass19.ppt15-213“The course that gives CMU its Zip!”215-213, S’08Keeping 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 listsDifferent free lists for different size classesMethod 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 26315-213, S’08Explicit Free ListsMaintain list(s) of free blocks, not all blocksThe “next” free block could be anywhereSo we need to store forward/back pointers, not just sizesStill need boundary tags for coalescingLuckily we track only free blocks, so we can use payload areaA B C4 4 4 4 66 44 4 4Forward linksBack linksABCNote: links are generally not in the same order as the blocks!415-213, S’08Allocating From Explicit Free ListsBefore:After:= malloc(…)(with splitting)515-213, S’08Freeing With Explicit Free ListsInsertion policy: Where in the free list do you put a newly freed block?LIFO (last-in-first-out) policyInsert freed block at the beginning of the free listPro: simple and constant timeCon: studies suggest fragmentation is worse than address ordered.Address-ordered policyInsert freed blocks so that free list blocks are always in address orderi.e. addr(pred) < addr(curr) < addr(succ) Con: requires search Pro: studies suggest fragmentation is lower than LIFO615-213, S’08Freeing With a LIFO Policy (Case 1)Insert the freed block at the root of the listfree( )RootRootBefore:After:715-213, S’08Freeing With a LIFO Policy (Case 2)Splice out predecessor block, coalesce both memory blocks, and insert the new block at the root of the listfree( )RootRootBefore:After:815-213, S’08Freeing With a LIFO Policy (Case 3)Splice out successor block, coalesce both memory blocks and insert the new block at the root of the listfree( )RootRootBefore:After:915-213, S’08Freeing 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 listfree( )RootRootBefore:After:1015-213, S’08Explicit List SummaryComparison to implicit list:Allocate is linear time in number of free blocks instead of total blocks -- Allocations 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 listSome extra space for the links (2 extra words needed for each block)Most common use of linked lists is in conjunction with segregated free listsKeep multiple linked lists of different size classes, or possibly for different types of objectsDoes this increase internal frag?1115-213, S’08Keeping 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 listDifferent free lists for different size classesMethod 4: Blocks sorted by sizeCan 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 261215-213, S’08Segregated List (Seglist) AllocatorsEach size class of blocks has its own free list1-2345-89-infOften have separate size class for each small size (2,3,4,…)For larger sizes typically have a size class for each power of 21315-213, S’08Seglist AllocatorGiven an array of free lists, each one for some size classTo allocate a block of size n:Search appropriate free list for block of size m > nIf an appropriate block is found:Split block and place fragment on appropriate list (optional)If no block is found, try next larger classRepeat until block is foundIf no block is found:Request additional heap memory from OS (using sbrk function)Allocate block of n bytes from this new memoryPlace remainder as a single free block in largest size class.1415-213, S’08Seglist Allocator (cont)To free a block:Coalesce and place on appropriate list (optional)Advantages of seglist allocatorsHigher throughput i.e., log time for power-of-two size classesBetter memory utilizationFirst-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.1515-213, S’08For More Info on AllocatorsD. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973The 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 surveyAvailable from CS:APP student site (csapp.cs.cmu.edu)1615-213, S’08Implicit 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 */}1715-213, S’08Garbage 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 conditionalsBut we can tell that certain blocks cannot be used if there are no pointers to themMust make certain assumptions about pointers1. Memory manager can distinguish pointers from non-pointers2. All pointers point to the start of a block 3. Cannot hide pointers (e.g., by coercing them to an int, and then back again)1815-213, S’08Classical GC AlgorithmsMark-and-sweep collection (McCarthy, 1960)Does not move blocks (unless you also “compact”)Reference counting (Collins, 1960)Does not move blocks (not discussed)Copying collection (Minsky, 1963)Moves blocks (not discussed)Generational Collectors (Lieberman and Hewitt, 1983)Collection based on lifetimesMost allocations become garbage very soonSo focus reclamation work on zones of memory recently allocatedFor more information, see Jones
View Full Document