Dynamic Memory Allocation II Apr 3, 2002AdminKeeping Track of Free BlocksExplicit Free ListsAllocating From Explicit Free ListsFreeing With Explicit Free ListsFreeing With a LIFO PolicySlide 8Freeing With a LIFO Policy (cont)Explicit List SummarySlide 11Segregated StorageSimple Segregated StorageSegregated FitsFor More Info on AllocatorsImplicit Memory Management: Garbage CollectionGarbage CollectionClassical GC algorithmsMemory as a GraphAssumptions For This LectureMark and Sweep CollectingMark and Sweep (cont.)Conservative Mark and Sweep in CGenerational CollectorsMemory-Related BugsDereferencing Bad PointersReading Uninitialized MemoryOverwriting MemorySlide 29Slide 30Slide 31Slide 32Referencing Nonexistent VariablesFreeing Blocks Multiple TimesReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Slide 37Dealing With Memory BugsDealing With Memory Bugs (cont.)Dynamic Memory Allocation IIApr 3, 2002TopicsExplicit doubly-linked free listsSegregated free listsGarbage collectionMemory-related perils and pitfallsclass21.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’03AdminConflicts for final.Please email before 4/8 so we can setup an alternative date.– 3 –15-213, F’03Keeping 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 26– 4 –15-213, F’03Explicit Free ListsUse data space for link pointersTypically doubly linkedStill need boundary tags for coalescingIt is important to realize that links are not necessarily in the same order as the blocksA B C4 4 4 4 66 44 4 4Forward linksBack linksABC– 5 –15-213, F’03Allocating From Explicit Free Listsfree blockpred succfree blockpred succBefore:After:(with splitting)– 6 –15-213, F’03Freeing 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 order»i.e. addr(pred) < addr(curr) < addr(succ) Con: requires search Pro: studies suggest fragmentation is better than LIFO– 7 –15-213, F’03Freeing With a LIFO PolicyCase 1: a-a-aInsert self at beginning of free listCase 2: a-a-fSplice out next, coalesce self and next, and add to beginning of free listselfa ap sselfa fbefore:p sfaafter:rootx– 8 –15-213, F’03Freeing With a LIFO PolicyCase 1: a-a-aInsert self at beginning of free listCase 2: a-a-fSplice out next, coalesce self and next, and add to beginning of free listselfa ap sselfa fbefore:p sfaafter:rootx– 9 –15-213, F’03Freeing With a LIFO Policy (cont)Case 3: f-a-aSplice out prev, coalesce with self, and add to beginning of free listCase 4: f-a-fSplice 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– 10 –15-213, F’03Explicit 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 listSome extra space for the links (2 extra words needed for each block)Main 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?– 11 –15-213, F’03Keeping 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 26– 12 –15-213, F’03Segregated StorageEach size class has its own collection of blocks1-2345-89-16Often have separate size class for every small size (2,3,4,…)For larger sizes typically have a size class for each power of 2– 13 –15-213, F’03Simple 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 pageallocate first block on listConstant timeTo free a block:Add to free listIf page is empty, return the page for use by another size (optional)Tradeoffs:Fast, but can fragment badly– 14 –15-213, F’03Segregated 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 > 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 foundTo free a block:Coalesce and place on appropriate list (optional)TradeoffsFaster search than sequential fits (i.e., log time for power of two size classes)Controls fragmentation of simple segregated storageCoalescing can increase search timesDeferred coalescing can help– 15 –15-213, F’03For 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)– 16 –15-213, F’03Implicit 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,
View Full Document