Memory Management II: Dynamic Storage Allocation Mar 7, 2000Basic allocator mechanismsSegregate StorageSimple segregated storageSegregated fitsBuddy systemsBuddy systems (cont)Slide 8Slide 9Internal fragmentationImplicit Memory Management Garbage collectorGarbage CollectionClassical GC algorithmsMemory as a graphAssumptions for this lectureMark and sweep collectingMark and sweep (cont.)Mark and sweep in CCopying collectionCopying collection (new)Copying collection (flip)Copying collection (copy)Reference countingSlide 24Slide 25Reference counting exampleSlide 27Slide 28Slide 29Reference counting cyclic data structuresGarbage Collection SummaryMemory Management II:Dynamic Storage Allocation Mar 7, 2000Topics•Segregated free lists–Buddy system•Garbage collection–Mark and Sweep–Copying–Reference countingclass15.ppt15-213“The course that gives CMU its Zip!”CS 213 S’00– 2 –class15.pptBasic allocator mechanismsSequential fits (implicit or explicit single free list)•best fit, first fit, or next fit placement•various splitting and coalescing options–splitting thresholds–immediate or deferred coalescingSegregated free lists•simple segregated storage -- separate heap for each size class•segregated fits -- separate linked list for each size class–buddy systemsCS 213 S’00– 3 –class15.pptSegregate StorageEach size “class” has its own collection of blocks1-2345-89-16•Often have separate collection for every small size (2,3,4,…)•For larger sizes typically have a collection for each power of 2CS 213 S’00– 4 –class15.pptSimple 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 page–allocate first block on list•constant timeTo 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 badlyCS 213 S’00– 5 –class15.pptSegregated 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 > 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 foundTo 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 helpCS 213 S’00– 6 –class15.pptBuddy systemsSpecial case of segregated fits.•all blocks are power of two sizesBasic idea:•Heap is 2m words•Maintain separate free lists of each size 2k, 0 <= k <= m.•Requested block sizes are rounded up to nearest power of 2.•Originally, one free block of size 2m.CS 213 S’00– 7 –class15.pptBuddy systems (cont)To allocate a block of size 2k:•Find first available block of size 2j s.t. k <= j <= m.•if j == k then done.•otherwise recursively split block until j == k. •Each remaining half is called a “buddy” and is placed on the appropriate free list2mbuddybuddybuddyCS 213 S’00– 8 –class15.pptBuddy systems (cont)To free a block of size 2k•continue coalescing with buddies while the buddies are freebuddybuddyBlock to freebuddyNot free, doneAdded to appropriate free listCS 213 S’00– 9 –class15.pptBuddy systems (cont)Key fact about buddy systems:•given the address and size of a block, it is easy to compute the address of its buddy•e.g., block of size 32 with address xxx...x00000 has buddy xxx...x10000Tradeoffs:•fast search and coalesce•subject to internal fragmentationCS 213 S’00– 10 –class15.pptInternal fragmentationInternal fragmentation is wasted space inside allocated blocks:•minimum block size larger than requested amount–e.g., due to minimum free block size, free list overhead•policy decision not to split blocks–e.g., buddy system –Much easier to define and measure than external fragmentation.CS 213 S’00– 11 –class15.pptImplicit Memory ManagementGarbage collectorGarbage 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, Perl, Mathematica, Variants (conservative garbage collectors) exist for C and C++•Cannot collect all garbagevoid foo() { int *p = malloc(128); return; /* p block is now garbage */}CS 213 S’00– 12 –class15.pptGarbage CollectionHow 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 themNeed 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)CS 213 S’00– 13 –class15.pptClassical GC algorithmsMark and sweep collection (McCarthy, 1960)•Does not move blocks (unless you also “compact”)Reference counting (Collins, 1960)•Does not move blocksCopying collection (Minsky, 1963)•Moves blocksFor more information see Jones and Lin, “Garbage Collection: Algorithms for Automatic Dynamic Memory”, John Wiley & Sons, 1996.CS 213 S’00– 14 –class15.pptMemory as a graphWe 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 nodesHeap nodesNot-reachable(garbage)reachableA 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)CS 213 S’00– 15 –class15.pptAssumptions for this lectureApplication•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 bEach block will have a header word•addressed as b[-1], for a block b•Used for different purposes in
View Full Document