Keeping Track of Free Blocks 15 213 Method 1 Implicit list using lengths links all blocks The course that gives CMU its Zip Dynamic Memory Allocation II April 1 2004 5 4 6 2 Method 2 Explicit list among the free blocks using pointers within the free blocks Topics 5 Explicit doubly linked free lists Segregated free lists Garbage collection Memory related perils and pitfalls 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 15 213 S 04 2 Explicit Free Lists A Allocating From Explicit Free Lists B C pred Use data space for link pointers Before Typically doubly linked Still need boundary tags for coalescing succ free block Forward links A 4 pred B 4 4 4 6 6 4 C 4 4 4 After with splitting Back links succ free block It is important to realize that links are not necessarily in the same order as the blocks 3 15 213 S 04 4 15 213 S 04 Freeing With Explicit Free Lists Freeing With a LIFO Policy Insertion policy Where in the free list do you put a newly freed block Case 1 a a a root LIFO last in first out policy x Insert self at beginning of free list Insert freed block at the beginning of the free list Pro simple and constant time Con studies suggest fragmentation is worse than address ordered a self a p 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 Case 2 a a f before a Splice out next coalesce self and next and add to beginning of free list self f p after a 15 213 S 04 5 Insert self at beginning of free list 15 213 S 04 s before x a f 6 p root Case 3 f a a self Splice out prev coalesce with self and add to beginning of free list a f p self a s after f p Case 2 a a f p1 a self Case 4 f a f p a s1 p2 s2 before f after a s before Splice out next coalesce self and next and add to beginning of free list s Freeing With a LIFO Policy cont Freeing With a LIFO Policy Case 1 a a a s f Splice out prev and next coalesce with self and add to beginning of list s p1 self s1 f p2 s2 after f f 7 15 213 S 04 8 15 213 S 04 Explicit List Summary Keeping Track of Free Blocks Comparison to implicit list Method 1 Implicit list using lengths links all blocks 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 Does this increase internal frag Main use of linked lists is in conjunction with segregated free lists 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 list Different free lists for different size classes Keep multiple linked lists of different size classes or possibly for different types of objects Method 4 Blocks sorted by size Can use a balanced tree e g Red Black tree with pointers within each free block and the length used as a key 15 213 S 04 9 15 213 S 04 10 Segregated Storage Simple Segregated Storage Each size class has its own collection of blocks Separate heap and free list for each size class No splitting 1 2 To allocate a block of size n If free list for size n is not empty 3 allocate first block on list note list can be implicit or explicit If free list is empty 4 get a new page create new free list from all blocks in page allocate first block on list 5 8 Constant time 9 16 To free a block Often have separate size class for every small size 2 3 4 For larger sizes typically have a size class 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 11 15 213 S 04 12 15 213 S 04 Segregated Fits For More Info on Allocators Array of free lists each one for some size class To allocate a block of size n D Knuth The Art of Computer Programming Second Edition Addison Wesley 1973 Search appropriate free list for block of size m n If an appropriate block is found The classic reference on dynamic storage allocation Split block and place fragment on appropriate list optional If no block is found try next larger class Repeat until block is found 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 Comprehensive survey Available from CS APP student site csapp cs cmu edu 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 15 213 S 04 13 Implicit Memory Management Garbage Collection 15 213 S 04 14 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 Garbage collection automatic reclamation of heapallocated storage application never has to free void foo int p malloc 128 return p block is now garbage Need to make certain assumptions about pointers Memory manager can distinguish pointers from nonpointers All pointers point to the start of a block Cannot hide pointers e g by coercing them to an int and then back again 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 However cannot necessarily collect all garbage 15 15 213 S 04 16 15 213 S 04 Memory as a Graph Classical GC algorithms We view memory as a directed graph Mark and sweep collection McCarthy 1960 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 Does not move blocks unless you also compact Reference counting Collins …
View Full Document