Slide 1TodayKeeping Track of Free BlocksExplicit Free ListsExplicit 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 SummaryKeeping Track of Free BlocksTodaySegregated List (Seglist) AllocatorsSeglist AllocatorSeglist Allocator (cont.)More Info on AllocatorsTodayImplicit Memory Management: Garbage CollectionGarbage CollectionClassical GC AlgorithmsMemory as a GraphMark and Sweep CollectingAssumptions For a Simple ImplementationMark and Sweep (cont.)Conservative Mark & Sweep in CTodayMemory-Related Perils and PitfallsC operatorsC Pointer Declarations: Test Yourself!Dereferencing Bad PointersReading Uninitialized MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryOverwriting MemoryReferencing Nonexistent VariablesFreeing Blocks Multiple TimesReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Failing to Free Blocks (Memory Leaks)Dealing With Memory BugsDealing With Memory Bugs (cont.)Carnegie Mellon1Dynamic Memory Allocation: Advanced Concepts15-213: Introduction to Computer Systems18th Lecture, Oct. 26, 2010Instructors: Randy Bryant and Dave O’HallaronCarnegie Mellon2TodayExplicit free listsSegregated free listsGarbage collectionMemory-related perils and pitfallsCarnegie Mellon3Keeping Track of Free BlocksMethod 1: Implicit free list using length—links all blocksMethod 2: Explicit free list among the free blocks using pointersMethod 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 26Carnegie Mellon4Explicit 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 areaSizePayload andpaddingaSize aSize aSize aNextPrevAllocated (as before)FreeCarnegie Mellon5Explicit Free ListsLogically:Physically: blocks can be in any orderA B C4 4 4 4 66 44 4 4Forward (next) linksBack (prev) linksABCCarnegie Mellon6Allocating From Explicit Free ListsBeforeAfter= malloc(…)(with splitting)conceptual graphicCarnegie Mellon7Freeing 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 orderedAddress-ordered policyInsert freed blocks so that free list blocks are always in address order: addr(prev) < addr(curr) < addr(next) Con: requires search Pro: studies suggest fragmentation is lower than LIFOCarnegie Mellon8Freeing With a LIFO Policy (Case 1)Insert the freed block at the root of the listfree( )RootRootBeforeAfterconceptual graphicCarnegie Mellon9Freeing 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( )RootRootBeforeAfterconceptual graphicCarnegie Mellon10Freeing 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( )RootRootBeforeAfterconceptual graphicCarnegie Mellon11Freeing 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( )RootRootBeforeAfterconceptual graphicCarnegie Mellon12Explicit List SummaryComparison to implicit list:Allocate is linear time in number of free blocks instead of all blocksMuch 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)Does this increase internal fragmentation?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 objectsCarnegie Mellon13Keeping Track of Free BlocksMethod 1: Implicit list using length—links all blocksMethod 2: Explicit list among the free blocks using pointersMethod 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 26Carnegie Mellon14TodayExplicit free listsSegregated free listsGarbage collectionMemory-related perils and pitfallsCarnegie Mellon15Segregated List (Seglist) AllocatorsEach size class of blocks has its own free listOften have separate classes for each small sizeFor larger sizes: One class for each two-power size1-2345-89-infCarnegie Mellon16Seglist 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())Allocate block of n bytes from this new memoryPlace remainder as a single free block in largest size class.Carnegie Mellon17Seglist Allocator (cont.)To free a block:Coalesce and place on appropriate list (optional)Advantages of seglist allocatorsHigher throughput 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.Carnegie Mellon18More Info on AllocatorsD. Knuth, “The Art of Computer Programming”, 2nd 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)Carnegie
View Full Document