15 213 The course that gives CMU its Zip Dynamic Memory Allocation II Nov 8 2001 Topics class22 ppt doubly linked free lists segregated free lists garbage collection memory related perils and pitfalls Keeping track of free blocks Method 1 implicit list using lengths links all blocks 5 4 6 2 Method 2 explicit list among the free blocks using pointers within the free blocks 5 4 6 2 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 within each free block and the length used as a key class22 ppt 2 CS 213 F 01 Explicit free lists A B C Use data space for link pointers Typically doubly linked Still need boundary tags for coalescing Forward links A 4 B 4 4 4 6 6 4 C 4 4 4 Back links It is important to realize that links are not necessarily in the same order as the blocks class22 ppt 3 CS 213 F 01 Allocating from explicit free lists pred Before succ free block pred After with splitting class22 ppt succ free block 4 CS 213 F 01 Freeing with explicit free lists Insertion policy Where to put the newly freed block in the 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 LIFO class22 ppt 5 CS 213 F 01 Freeing with a LIFO policy pred p succ s Case 1 a a a a insert self at beginning of free list self a p s before Case 2 a a f a self f splice out next coalesce self and next and add to beginning of free list p after a class22 ppt 6 f CS 213 F 01 s Freeing with a LIFO policy cont p Case 3 f a a s before f splice out prev coalesce with self and add to beginning of free list p self a s after f p1 Case 4 f a f a s1 p2 before splice out prev and next coalesce with self and add to beginning of list f p1 self s1 f p2 after f class22 ppt s2 7 CS 213 F 01 s2 Explicit list summary Comparison 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 list Some extra space for the links 2 extra words needed for each block Main use of linked lists is in conjunction with segregated free lists Keep multiple linked lists of different size classes or possibly for different types of objects class22 ppt 8 CS 213 F 01 Segregated Storage Each size class has its own collection of blocks 1 2 3 4 5 8 9 16 Often have separate collection for every small size 2 3 4 For larger sizes typically have a collection for each power of 2 class22 ppt 9 CS 213 F 01 Simple segregated storage Separate heap and free list for each size class No splitting To 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 time To 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 badly class22 ppt 10 CS 213 F 01 Segregated fits Array of free lists each one for some size class To 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 found To free a block coalesce and place on appropriate list optional Tradeoffs faster search than sequential fits i e log time for power of two size classes controls fragmentation of simple segregated storage coalescing can increase search times deferred coalescing can help class22 ppt 11 CS 213 F 01 For more information of dynamic storage allocators D Knuth The Art of Computer Programming Second Edition Addison Wesley 1973 the classic reference on dynamic storage allocation Wilson et al Dynamic Storage Allocation A Survey and Critical Review Proc 1995 Int l Workshop on Memory Management Kinross Scotland Sept 1995 comprehensive survey available from the course web page see Documents page class22 ppt 12 CS 213 F 01 Implicit Memory Management Garbage collector Garbage collection automatic reclamation of heapallocated storage application never has to free void foo int p malloc 128 return p block is now garbage Common in functional languages scripting languages and modern object oriented languages Lisp ML Java Perl Mathematica Variants conservative garbage collectors exist for C and C Cannot collect all garbage class22 ppt 13 CS 213 F 01 Garbage Collection How 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 conditionals But we can tell that certain blocks cannot be used if there are no pointers to them Need 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 back again class22 ppt 14 CS 213 F 01 Classical GC algorithms Mark 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 Garbage Collection Algorithms for Automatic Dynamic Memory John Wiley Sons 1996 class22 ppt 15 CS 213 F 01 Memory as a graph We 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 called root nodes e g registers locations on the stack global variables Root nodes reachable Heap nodes Not reachable garbage A node block is reachable if there is a path from any root to that node Non reachable nodes are garbage never needed by the application class22 ppt 16 CS 213 F 01 Assumptions for this lecture Application new n returns pointer to new block with all locations cleared read b i read location i of block b into register write b i v write v into location i of block b …
View Full Document