Keeping track of free blocks 15 213 Method 1 implicit list using lengths links all blocks The course that gives CMU its Zip 5 Dynamic Memory Allocation II Nov 8 2001 6 4 6 2 Method 3 segregated free lists doubly linked free lists segregated free lists garbage collection memory related perils and pitfalls 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 class22 ppt Explicit free lists A 2 Method 2 explicit list among the free blocks using pointers within the free blocks 5 Topics 4 B 2 CS 213 F 01 Allocating from explicit free lists C pred Use data space for link pointers Typically doubly linked Still need boundary tags for coalescing Before succ free block Forward links A 4 B 4 4 4 6 6 4 C 4 4 4 pred succ Back links After with splitting free block It is important to realize that links are not necessarily in the same order as the blocks class22 ppt 3 CS 213 F 01 class22 ppt 4 CS 213 F 01 Freeing with explicit free lists Freeing with a LIFO policy 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 pred p succ s Case 1 a a a a insert self at beginning of free list self a p 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 5 class22 ppt CS 213 F 01 Freeing with a LIFO policy cont p Case 3 f a a s f p 6 s f CS 213 F 01 Explicit list summary Comparison to implicit list before splice out prev coalesce with self and add to beginning of free list s self 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 a s after f a Main use of linked lists is in conjunction with segregated free lists p1 Case 4 f a f p2 s1 s2 before splice out prev and next coalesce with self and add to beginning of list f p1 self s1 Keep multiple linked lists of different size classes or possibly for different types of objects f p2 s2 after f class22 ppt 7 CS 213 F 01 class22 ppt 8 CS 213 F 01 Segregated Storage Simple segregated storage Each size class has its own collection of blocks 1 2 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 3 4 5 8 9 16 To free a block Often have separate collection for every small size 2 3 4 For larger sizes typically have a collection for each power of 2 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 9 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 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 11 10 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 To free a block class22 ppt class22 ppt CS 213 F 01 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 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 Common in functional languages scripting languages and modern object oriented languages 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 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 class22 ppt Classical GC algorithms Mark and sweep collection McCarthy 1960 Does not move blocks unless you also compact 14 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 Reference counting Collins 1960 Does not move blocks not discussed Copying collection Minsky 1963 Root nodes Moves blocks not discussed For more information see Jones and Lin Garbage Collection Algorithms for Automatic Dynamic Memory John Wiley Sons 1996 Heap nodes reachable 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 15 CS 213 F 01 class22 ppt 16 CS 213 F 01 Assumptions for this lecture Mark and sweep collecting Can build on top of malloc free package Application new n returns pointer to new block with all locations cleared read b i read location i of block b into register …
View Full Document