1 / 41

Memory Management (Ch 14)

It is a 800-pages topic, but here we want to do it in one/two class!. Memory Management (Ch 14). Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager Activation records Objects Explicit allocations: new , malloc , etc.

quana
Download Presentation

Memory Management (Ch 14)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. It is a 800-pages topic, but here we want to do it in one/two class! Memory Management (Ch 14) • Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager • Activation records • Objects • Explicit allocations: new, malloc, etc. • Implicit allocations: strings, file buffers, arrays with dynamically varying size, etc. IT 327

  2. 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 • Our textbook shows an examples of memory management code in Java IT 327

  3. 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 IT 327

  4. A Stack Illustration An empty stack of 8 words. reg. grow IT 327

  5. Some program P needs 3 words The manager program calls m.push(3). P gets this pointer IT 327

  6. Some other program Q needs 2 words The manager program calls m.push(2). P gets this pointer Q gets this pointer wasted IT 327

  7. Heap • Stack is easy to do, but Allocations and deallocations may come in any order? • A heapis 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… IT 327

  8. How to deal blocks to the requests? A pool of free blocks are linked, initially containing one big free block, then fragmented into small blocks The easiest approach: First Fitfor 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 IT 327

  9. Heap implementation in an array Pointer to next block, -1 means no next block. Size of this block IT 327

  10. Some program P requests 4 words Size of this free block P gets this pointer Size of this block for P IT 327

  11. Some program Q requests 2 words Q gets this pointer Size of this block IT 327

  12. P is done IT 327

  13. Some program R requests 1 word R gets this pointer IT 327

  14. top A Problem p2 p2 • Consider this sequence: • The manager needs to coalesce adjacent free blocks. p1=m.allocate(4); p2=m.allocate(4); m.deallocate(p1); m.deallocate(p2); p3=m.allocate(7); top top p1 p1 top How? IT 327

  15. Improvement strategy • Quick lists, a separate list, same sized blocks • Delayed coalescing Keep separate free lists for popular (small) block sizes IT 327

  16. 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. DeFragmentation IT 327

  17. Heap Mechanisms (to manage heaps.) Many variety and different refinements • Three major issues: • Placement—where to allocate a block • Splitting—when and how to split large blocks • Coalescing—when and how to recombine IT 327

  18. 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 IT 327

  19. 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 somemultiple, for example 2n IT 327

  20. Coalescing: When and how to recombine adjacent free blocks • Several varieties: • No coalescing • Eager coalescing • Delayed coalescing IT 327

  21. Current Heap Links • Some systems track current heap links • A current heap link is a memory address (location in the heap) where the running program may use a b What if a = new X(i); b = new X(j); a = b; IT 327

  22. Tracing Current Heap Links 2 IntList a = new IntList(null);int b = 2;int c = 1;a = a.cons(b);a = a.cons(c); 1 3 Where are the current heap links in this picture? 1 2 3 IT 327

  23. 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. • 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 {a b c } They are not in the active activation record. IT 327

  24. Possible Errors In Current Heap Links Heap 1. Exclusion errors: a memory location that actually is a current heap link is left out. (This type of error is not allowed) Current heap links Static variables p, r, a Current Activation Record IT 327

  25. Possible Errors In Current Heap Links Heap 2. Unused inclusion errors: a memory location is included, but the program never actually uses the value stored there Current heap links Static variables s, p, r, a, c, d How do I know this is the case? Current Activation Record IT 327

  26. Possible Errors In Current Heap Links Heap 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) Current heap links Static variables s, p, r, a, c, y, z How do I know this is the case? Current Activation Record IT 327

  27. 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 inclusionerrors unavoidable • Depending on the language, used inclusions errors (mistaken) may also be unavoidable IT 327

  28. An application for current heap links:Heap Compaction/Defragmentation P P • Manager can re-allocate blocks: • Copy the block to a new location • Update all links to (or into) that block • So it can compact the heap, moving all allocated blocks to one end, leaving one big free block and no fragmentation S Q R Q T R S T IT 327

  29. There are so many errors caused by improper de-allocation (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 IT 327

  30. Garbage Collection • Mark and sweep • Copying • Reference counting Three Major Approaches IT 327

  31. 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 IT 327

  32. Mark And Sweep blocks are not moved / fragmentations may remain Mark: live heap links (in red) P P Free blocks Free blocks R R S S Sweep: return unmarked blocks to the free pool T T IT 327

  33. 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 IT 327

  34. Moves blocks: cannot tolerate used inclusion errors (mistake) X=100 P X=700 P S Copying Collection  Q R Q T R S T IT 327

  35. 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 IT 327

  36. Reference Counting IT 327

  37. Garbage cycles can’t be identified Reference Counting Problem Reference counters are not zero But all blocks are garbage. IT 327

  38. Reference Counting • Problem 1: cycles of garbage • Problem 2: overhead of updating reference counters is high • One advantage: more uniform. I.e., cost is incremental, no big pause while collecting IT 327

  39. Garbage Collecting Refinements • Generational collectors • Divide block into generations according to age • Garbage collect in younger generations more often (using previous methods) • Incremental collectors • Collect garbage a little at a time • Avoid the uneven performance like mark-and-sweep and copying collectors IT 327

  40. Languages with Garbage Collectors • Required: Java, ML • encouraged: Ada • difficult to have: C, C++ • But not impossible, even for C and C++ • There are libraries that replace the usual malloc/free with a garbage-collecting manager IT 327

  41. 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 IT 327

More Related