Dynamic Memory Allocation II Nov 8, 2001Topics• doubly-linked free lists• segregated free lists• garbage collection• memory-related perils and pitfallsclass22.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class22.pptKeeping track of free blocks• Method 1: implicit list using lengths -- links all blocks• Method 2: explicit list among the free blocks usingpointers 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 withineach free block, and the length used as a key54 2654 26CS 213 F’01– 3 –class22.pptExplicit 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 the sameorder as the blocksA B C4 4 4 4 66 44 4 4Forward linksBack linksABCCS 213 F’01– 4 –class22.pptAllocating from explicit free listsfree blockpred succfree blockpred succBefore:After:(with splitting)CS 213 F’01– 5 –class22.pptFreeing with explicit free listsInsertion policy: Where to put the newly freed block inthe free list• 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 LIFOCS 213 F’01– 6 –class22.pptFreeing with a LIFO policyCase 1: a-a-a• insert self at beginning offree listCase 2: a-a-f• splice out next, coalesceself and next, and add tobeginning of free listpred (p) succ (s)selfa ap sselfa fbefore:p sfaafter:CS 213 F’01– 7 –class22.pptFreeing with a LIFO policy (cont)Case 3: f-a-a• splice out prev, coalescewith self, and add tobeginning of free listCase 4: f-a-f• splice out prev and next,coalesce with self, and addto beginning of listp sselff abefore:p sf aafter:p1 s1selff fbefore:fafter:p2 s2p1 s1 p2 s2CS 213 F’01– 8 –class22.pptExplicit list summaryComparison to implicit list:• Allocate is linear time in number of free blocks instead of totalblocks -- much faster allocates when most of the memory is full• Slightly more complicated allocate and free since needs to spliceblocks in and out of the list• Some extra space for the links (2 extra words needed for eachblock)Main use of linked lists is in conjunction withsegregated free lists• Keep multiple linked lists of different size classes, or possibly fordifferent types of objectsCS 213 F’01– 9 –class22.pptSegregated StorageEach size “class” has its own collection of blocks1-2345-89-16• Often have separate collection for every small size (2,3,4,…)• For larger sizes typically have a collection for each power of 2CS 213 F’01– 10 –class22.pptSimple 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 badlyCS 213 F’01– 11 –class22.pptSegregated 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 sizeclasses)• controls fragmentation of simple segregated storage• coalescing can increase search times–deferred coalescing can helpCS 213 F’01– 12 –class22.pptFor more information of dynamic storage allocatorsD. Knuth, “The Art of Computer Programming, SecondEdition”, Addison Wesley, 1973• the classic reference on dynamic storage allocationWilson et al, “Dynamic Storage Allocation: A Surveyand Critical Review”, Proc. 1995 Int’l Workshop onMemory Management, Kinross, Scotland, Sept, 1995.• comprehensive survey• available from the course web page (see Documents page)CS 213 F’01– 13 –class22.pptImplicit Memory ManagementGarbage collectorGarbage 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 Cand C++• Cannot collect all garbagevoid foo() { int *p = malloc(128); return; /* p block is now garbage */}CS 213 F’01– 14 –class22.pptGarbage CollectionHow does the memory manager know when memorycan be freed?• In general we cannot know what is going to be used in the futuresince it depends on conditionals• But we can tell that certain blocks cannot be used if there are nopointers 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 backagain)CS 213 F’01– 15 –class22.pptClassical 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)For more information see Jones and Lin, “GarbageCollection: Algorithms for Automatic DynamicMemory”, John Wiley & Sons, 1996.CS 213 F’01– 16 –class22.pptMemory as a graphWe view memory as a directed graph• Each block is a node in the graph• Each pointer is an edge in the graph• Locations not in the heap that contain pointers into the heap are calledroot nodes (e.g. registers, locations on the stack, global variables)Root nodesHeap nodesNot-reachable(garbage)reachableA node (block) is
View Full Document