Memory Management (Ch 14) It is a 800-pages topic, but here we want to do it in one/two class! Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – – – – 4/13/2015 Activation records Objects Explicit allocations: new, malloc, etc. Implicit allocations: strings, file buffers, arrays with dynamically varying size, etc. IT 327 1 Memory Model Yes! It’s OS’s job!! For now, assume that the OS grants each running program one or more fixed-size regions of memory for dynamic allocation We will model these regions as an array – 4/13/2015 Our textbook shows an examples of memory management code in Java IT 327 2 Stacks Of Activation Records A simple memory management problem Activation records must be allocated dynamically Allocate on call and de-allocate on return Stack of activation records: push and pop 4/13/2015 IT 327 3 A Stack Illustration top: 8 An empty stack of 8 words. 7: reg. 6: 5: 4: 3: grow 2: 1: 0: 4/13/2015 IT 327 4 Some program P needs 3 words The manager program calls m.push(3). top: 4 7: first activation record 6: P gets this pointer 5: 4: 8 3: 2: 1: 0: 4/13/2015 IT 327 5 Some other program Q needs 2 words The manager program calls m.push(2). top: 1 7: first activation record 6: P gets this pointer 5: 4: 3: Q gets this pointer 2: 1: 8 second activation record 4 0: wasted 4/13/2015 IT 327 6 Heap Stack is easy to do, but Allocations and deallocations may come in any order? A heap is a pool of blocks of memory At the beginning there is only one block (but not always the case) There are many approaches for doing this… 4/13/2015 IT 327 7 A pool of free blocks are linked, initially containing one big free block, then fragmented into small blocks How to deal blocks to the requests? The easiest approach: First Fit for request – – – Search the free list for first adequate block If there is extra space in the block, return the unused portion to front of the free list Allocate requested portion (at the lower end) To free a block, just add to the front of the free list 4/13/2015 IT 327 8 Heap implementation in an array 9: 8: 7: 6: 5: 4: 3: 2: freeStart: 0 1: -1 0: 10 Pointer to next block, -1 means no next block. Size of this block 4/13/2015 IT 327 9 9: 8: 7: Some program P requests 4 words P gets this pointer 6: -1 5: 5 Size of this free block 4: 3: first allocated block 2: 1: freeStart: 5 4/13/2015 0: 5 Size of this block for P IT 327 10 Some program Q requests 2 words Q gets this pointer 9: -1 8: 2 7: Size of this block 6: second allocated block 5: 3 4: 3: first allocated block 2: 1: freeStart: 8 4/13/2015 IT 327 0: 5 11 9: 8: 7: -1 2 6: second allocated block 5: 3 2 6: second allocated block 5: 3 3: first allocated block 2: 2: P is done 1: 4/13/2015 8: 4: 3: 0: -1 7: 4: freeStart: 8 9: 5 freeStart: 0 IT 327 1: 8 0: 5 12 9: -1 8: 2 7: Some program R requests 1 word 6: second allocated block 5: 3 4: R gets this pointer 3: 8 2: 3 third allocated block 1: freeStart: 2 4/13/2015 IT 327 0: 2 13 top A Problem Consider this sequence: 1. p1=m.allocate(4); 2. p2=m.allocate(4); p2 -1 5 top 5 p2 5 0 5 -1 5 -1 5 top 3. m.deallocate(p1); 4. m.deallocate(p2); 5. p3=m.allocate(7); The manager needs to coalesce adjacent free blocks. How? 4/13/2015 p1 5 p1 5 IT 327 top 14 Improvement strategy Quick lists, a separate list, same sized blocks Keep separate free lists for popular (small) block sizes Delayed coalescing 4/13/2015 IT 327 15 Another problem: Fragmentation p1=m.allocate(4); p2=m.allocate(1); m.deallocate(p1); p3=m.allocate(5); The final allocation will fail because of fragmentation. 9: 8: -1 7: 3 6: second allocated block 5: 2 4: 3: 2: DeFragmentation freeStart: 0 4/13/2015 IT 327 1: 7 0: 5 16 Heap Mechanisms (to manage heaps.) Many variety and different refinements Three major issues: – – – 4/13/2015 Placement—where to allocate a block Splitting—when and how to split large blocks Coalescing—when and how to recombine IT 327 17 Placement: Where to allocate a block (FF) First Fit from a stack (FIFO) of free list (BF) Best Fit … (NF) Next Fit … Some mechanisms use a more scalable data structure like a Balanced Binary Tree 4/13/2015 IT 327 18 Splitting: When and how to split large blocks Split to requested size Sometimes, less splitting gives better results e.g., allocate more than requested or rounding up allocation size to some multiple, for example 2n 4/13/2015 IT 327 19 Coalescing: When and how to recombine adjacent free blocks Several varieties: – – – 4/13/2015 No coalescing Eager coalescing Delayed coalescing IT 327 20 Current Heap Links Some systems track current heap links A current heap link is a memory a address (location in the heap) where the running program b may use What if a = new X(i); b = new X(j); a = b; 4/13/2015 IT 327 21 Tracing Current Heap Links start: a: b: 2 c: 1 1 (an IntList) free (activation record for main) head: 2 tail: null 3 free 2 (a ConsCell) free head: 1 tail: (a ConsCell) IntList a = new IntList(null); int b = 2; int c = 1; a = a.cons(b); a = a.cons(c); Where are the current heap links in this picture? 1 2 3 free the stack 4/13/2015 the heap free IT 327 22 To Find Current Heap Links Start with the root set: memory locations outside of the heap with links into the heap We have to check: – Active activation records (if on the stack) – Static variables, etc. {a b c } They are not in the active activation record. 4/13/2015 For each memory location in the set, look at the allocated block it points to, and add all the memory locations in that block Repeat until no new locations are found IT 327 23 Possible Errors In Current Heap Links 1. Exclusion errors: a memory location that actually is a current heap link is left out. (This type of error is not allowed) Heap Current heap links Static variables s 456 x:123 a y:100 b p, r, a c d 2000 Current Activation Record p 3000 z:200 r[3] 4/13/2015 4000 IT 327 24 Possible Errors In Current Heap Links 2. Unused inclusion errors: a memory location is included, but the program never actually uses the value stored there Heap Current heap links Static variables s 456 x:123 a y:100 b s, p, r, a, c, d c d How do I know this is the case? 2000 Current Activation Record p 3000 z:200 r[3] 4/13/2015 4000 IT 327 25 Possible Errors In Current Heap Links 3. Used inclusion errors (mistaken): a memory location is included, but the program uses the value stored there as something other than a heap address (e.g. int) Heap Current heap links Static variables s 456 x:123 a y:100 b s, p, r, a, c, y, z c d How do I know this is the case? 2000 Current Activation Record p 3000 z:200 r[3] 4/13/2015 4000 IT 327 26 Errors Are Unavoidable For heap manager purposes, exclusion errors are unacceptable Therefore, we must include a location if it might be used as a heap link, and This makes unused inclusion errors unavoidable Depending on the language, used inclusions errors (mistaken) may also be unavoidable 4/13/2015 IT 327 27 An application for current heap links: Heap Compaction/Defragmentation P Manager can re-allocate blocks: – Copy the block to a new location – Update all links to (or into) that block P Q S R Q T R S So it can compact the heap, moving all allocated blocks to one end, leaving one big free block and no fragmentation T 4/13/2015 IT 327 28 There are so many errors caused by improper deallocation (e.g., Bad or without class destructors in C/C++) It is a burden on the programmer … Therefore, let the language system do the job automatically --- Garbage Collection 4/13/2015 IT 327 29 Garbage Collection Three Major Approaches 1. 2. 3. 4/13/2015 Mark and sweep Copying Reference counting IT 327 30 Mark And Sweep uses current heap links in a two-stage process: – Mark: find the live heap links and mark all the heap blocks pointed by them – Sweep: make a pass over the heap and return unmarked blocks to the free pool - both kinds of inclusion errors are tolerated, - blocks are not moved, fragmentation may remain 4/13/2015 IT 327 31 Mark And Sweep Mark: live heap links (in red) blocks are not moved / fragmentations may remain P P Free blocks Free blocks R S R S T T Sweep: return unmarked blocks to the free pool 4/13/2015 IT 327 32 Copying Collection divide memory in half, and uses only one half at a time When one half becomes full, find live heap links, and copy live blocks to the other half Fragmentation is eliminated Moves blocks Cannot tolerate used inclusion errors. Why? Because that used variable will be mistaken as a link and hence modified 4/13/2015 IT 327 33 Moves blocks: cannot tolerate used inclusion errors (mistake) X=100 X=700 P P Q Copying Collection S R Q T R S T 4/13/2015 IT 327 34 Reference Counting Each block has a counter of heap links to it Incremented when a heap link is copied, decremented when a heap link is discarded When counter goes to zero, block is garbage and can be freed Does not use current heap links 4/13/2015 IT 327 35 Reference Counting circle: 2 link: (activation record) free 1 link: free free link: 1 free the stack the heap 4/13/2015 IT 327 36 Garbage cycles can’t be identified Reference Counting Problem circle: null 1 link: (activation record) free 1 link: free But all blocks are garbage. free 1 link: the stack Reference counters are not zero free the heap 4/13/2015 IT 327 37 Reference Counting Problem 1: cycles of garbage Problem 2: overhead of updating reference counters is high 4/13/2015 One advantage: more uniform. I.e., cost is incremental, no big pause while collecting IT 327 38 Garbage Collecting Refinements Generational collectors – – Incremental collectors – – 4/13/2015 Divide block into generations according to age Garbage collect in younger generations more often (using previous methods) Collect garbage a little at a time Avoid the uneven performance like mark-andsweep and copying collectors IT 327 39 Languages with Garbage Collectors Required: Java, ML encouraged: Ada difficult to have: C, C++ – – 4/13/2015 But not impossible, even for C and C++ There are libraries that replace the usual malloc/free with a garbage-collecting manager IT 327 40 Conclusion Memory management is an important hidden player in language systems Performance and reliability are critical Different techniques are difficult to compare, since every run of every program makes different memory demands Still an active area of language systems research and experimentation 4/13/2015 IT 327 41