Dynamic Memory Allocation II October 21, 2008Summary of Key Allocator PoliciesKeeping Track of Free BlocksExplicit Free ListsAllocating From Explicit Free ListsFreeing With Explicit Free ListsFreeing With a LIFO Policy (Case 1)Freeing With a LIFO Policy (Case 2)Freeing With a LIFO Policy (Case 3)Freeing With a LIFO Policy (Case 4)Explicit List SummarySlide 12Segregated List (Seglist) AllocatorsSeglist AllocatorSeglist Allocator (cont)For More Info on AllocatorsMemory-Related Perils and PitfallsDereferencing Bad PointersReading Uninitialized MemoryOverwriting MemorySlide 21Slide 22Slide 23Referencing Nonexistent VariablesFreeing Blocks Multiple TimesImplicit List: CoalescingReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Slide 29Dealing With Memory BugsDealing With Memory Bugs (cont.)Implicit Memory Management: Garbage CollectionGarbage CollectionClassical GC AlgorithmsMemory as a GraphAssumptions For This LectureMark and Sweep CollectingMark and Sweep (cont.)Conservative Mark & Sweep in CDynamic Memory Allocation IIOctober 21, 2008TopicsExplicit doubly-linked free listsSegregated free listsGarbage collectionReview of pointersMemory-related perils and pitfallslecture-16.ppt15-213“The course that gives CMU its Zip!”215-213, F’08Summary of Key Allocator PoliciesPlacement policy:First fit, next fit, best fit, etc.Trades off lower throughput for less fragmentationInteresting observation: segregated free lists (next lecture) 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. e.g.,Coalesce as you scan the free list for malloc.Coalesce when the amount of external fragmentation reaches some threshold.315-213, F’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 26415-213, F’08Explicit Free ListsMaintain list(s) of free blocks, not all blocksThe “next” free block could be anywhereSo we need to store 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 don’t have to be in the same order as the blocks!515-213, F’08Allocating From Explicit Free ListsBefore:After:= malloc(…)(with splitting)7615-213, F’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 LIFO715-213, F’08Freeing With a LIFO Policy (Case 1)Insert the freed block at the root of the listfree( )5RootRootBefore:After:815-213, F’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:915-213, F’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:1015-213, F’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:1115-213, F’08Explicit List SummaryComparison to implicit list:Allocate is linear time in # of free blocks instead of total blocksAllocations 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 free 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?1215-213, F’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 261315-213, F’08Segregated List (Seglist) AllocatorsEach size class of blocks has its own free list1-2345-89-inf Often have separate size class for each small size (2,3,4,…) For larger sizes, typically have a size class for each power of 21415-213, F’08Seglist AllocatorGiven an array of free lists for different size classesTo 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())Allocate block of n bytes from this new memoryPlace remainder as a single free block in largest size class1515-213, F’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 heapExtreme case: Giving each block its own size class is equivalent to best-fit1615-213, F’08For More Info on AllocatorsD. Knuth, “The Art of Computer Programming, Second Edition”, Addison Wesley, 1973The classic reference on dynamic
View Full Document