Harvey Mudd CS 105 - Dynamic Memory Allocation II (36 pages)

Previewing pages 1, 2, 17, 18, 19, 35, 36 of 36 page document View the full content.
View Full Document

Dynamic Memory Allocation II



Previewing pages 1, 2, 17, 18, 19, 35, 36 of actual document.

View the full content.
View Full Document
View Full Document

Dynamic Memory Allocation II

89 views


Pages:
36
School:
Harvey Mudd College
Course:
Cs 105 - Computer Systems
Computer Systems Documents
Unformatted text preview:

CS 105 Tour of the Black Holes of Computing Dynamic Memory Allocation II Topics Explicit 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 20 16 24 8 Method 2 Explicit list among the free blocks using pointers within the free blocks 20 16 24 8 Method 3 Segregated free list Different free lists for different size classes Method 4 Blocks sorted by size not discussed 2 For example balanced tree Red Black with pointers inside each free block block length used as key CS 105 Explicit Free Lists A B C Use data space for link pointers Typically doubly linked Still need boundary tags for coalescing Forward links A 16 16 16 16 24 24 16 C 3 16 16 B 16 Back links Links aren t necessarily in same order as blocks CS 105 Allocating From Explicit Free Lists pred Before succ free block pred After with splitting 4 succ free block CS 105 Freeing With Explicit Free Lists Insertion policy Where in free list to put newly freed block LIFO last in first out policy Insert freed block at beginning of free list Pro simple and constant time Con studies suggest fragmentation is worse than address ordered Address ordered policy Insert freed blocks so list is always in address order i e addr pred addr curr addr succ Con requires search using boundary tags Pro studies suggest fragmentation is better than LIFO 5 CS 105 Freeing With a LIFO Policy pred p Case 1 a a a Insert self at beginning of free list a succ s self a p Case 2 a a f before a Remove next from free list coalesce self and next and add to beginning of free list after 6 s self f p a s f CS 105 Freeing With a LIFO Policy cont p Case 3 f a a Remove prev from free list coalesce with self and add to beginning of free list s before f after p 7 Remove prev and next from free list coalesce with self and add to beginning of list a s f p1 Case 4 f a f self a s1 p2 s2 before f after p1 self s1 f p2 s2 f CS 105 Summary of Explicit Lists Comparison to implicit lists Allocate is linear time in number of free blocks instead of total blocks much faster when most of memory full Slightly more complicated allocate and free since needs to splice blocks in and out of free list Some extra space for links 2 extra words per block Main use of linked lists is in conjunction with segregated free lists 8 Keep multiple linked lists of different size classes or possibly for different types of objects CS 105 Keeping Track of Free Blocks Method 1 Implicit list using lengths links all blocks 20 16 24 8 Method 2 Explicit list among the free blocks using pointers within the free blocks 20 16 24 8 Method 3 Segregated free list Different free lists for different size classes Method 4 Blocks sorted by size not discussed 9 For example balanced tree Red Black with pointers inside each free block block length used as key CS 105 Segregated Storage Each size class has its own collection of blocks 4 8 12 16 20 32 36 64 10 Often separate size class for every small size 8 12 16 For larger typically have size class for each power of 2 CS 105 Simple Segregated Storage Separate heap and free list for each size class No splitting To allocate block of size n If free list for size n is not empty Allocate first block on list can be implicit or explicit If free list is empty Get new page Create new free list from all blocks in page Allocate first block on list Constant time To free block Add to free list If page empty return it for use by another size optional Tradeoffs 11 Fast but can fragment badly CS 105 Segregated Fits Array of free lists one for each size class To allocate block of size n Search appropriate list for block of size m n If block found split and put fragment on smaller list optional If no block found try next larger class and repeat If largest class empty allocate page s big enough to hold desired block put remainder on appropriate list To free a block Coalesce and put on appropriate list Tradeoffs 12 Faster search than sequential fits log time for power of two size classes Controls fragmentation of simple segregated storage Coalescing can increase search times Deferred coalescing can help CS 105 Buddy Allocators Special case of segregated fits Basic idea Limited to power of two sizes Can only coalesce with buddy who is other half of next higher power of two Clever use of low address bits to find buddies Problem large powers of two result in large internal fragmentation e g what if you want to allocate 65537 bytes 13 CS 105 For More Info on Allocators D Knuth The Art of Computer Programming Second Edition Addison Wesley 1973 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 14 Comprehensive survey Available from CS APP student site csapp cs cmu edu CS 105 Implicit Memory Management Garbage Collection 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 Python Mathematica Variants conservative garbage collectors exist for C and C 15 Cannot collect all garbage CS 105 Garbage Collection How does memory manager know when memory can be freed In general can t know what will be used in future since depends on conditionals But we know certain blocks can t be used if there are no pointers to them Need to make certain assumptions about pointers 16 Memory manager can distinguish pointers from nonpointers All pointers point to start of block Can t hide pointers e g by coercing them to an int and then back again CS 105 Classical GC algorithms Mark and sweep collection McCarthy 1960 Doesn t move blocks unless you also compact Reference counting Collins 1960 Doesn t move blocks not discussed Copying collection Minsky 1963 Moves blocks not discussed Multiprocessing compactifying Steele 1975 For more information see Jones and Lin Garbage Collection Algorithms for Automatic Dynamic Memory John Wiley Sons 1996 17 CS 105 Memory as a Graph Think of memory as directed graph Each block is node in graph Each pointer is edge Locations not in heap that contain pointers into heap are called root nodes e g registers locations on stack global variables Root nodes Heap nodes Reachable Not reachable garbage Node block is reachable if there is


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Dynamic Memory Allocation II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dynamic Memory Allocation II and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?