consider n.
Skip this Video
Loading SlideShow in 5 Seconds..
Consider PowerPoint Presentation


2 Views Download Presentation
Download Presentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Consider Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p2 (30 k) Allocate p3 (40 k) Free p2 Allocate p4 (40 k) Free p3 Allocate p5 (60 k) Free p1 Allocate p6 (30k)

  2. Design YOUR algorithm for allocation and deallocation of memory Memory Allocation Algorithms

  3. Memory Management Dynamic (heap) Significant issues Significant execution time (16%) Memory performance not uniform Allocation policies Bugs Dereference problems Memory leaks

  4. Memory Allocation Strategies Explicit vs. Implicit Memory Allocator General purpose vs. custom allocator Software vs. hardware

  5. Allocation Examples p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2)

  6. Goals of Good malloc/free Good execution-time performance Good space utilization Good locality properties

  7. Fragmentation Poor memory utilization --- fragmentation Internal – overhead associated with a block of memory External – have enough blocks of memory for a request, but not contiguous Internal fragmentation Space in use

  8. External Fragmentation p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) External fragmentation depends on future requests; thus difficult to anticipate

  9. Bidirectional Coalescing Boundary tags [Knuth73] Replicate size/allocated word at bottom of free blocks Allows us to traverse the “list” backwards, but requires extra space Important and general technique!

  10. Boundary Tags 1 word a = 1: allocated block a = 0: free block size: total block size Application memory (allocated blocks only) Header size a Application Memory (and padding?) Format of allocated and free blocks Boundary tag (footer) size a 4 4 4 4 6 6 4 4

  11. Your turn Using boundary tag data structure, define algorithms for: Allocation Free

  12. Key Allocator Policies Placement policy: First fit, worst fit, best fit, etc. Trades off lower throughput for less fragmentation Splitting policy: When do we go ahead and split free blocks? How much internal fragmentation are we willing to tolerate? Coalescing policy: Immediate coalescing: coalesce adjacent blocks each time free is called Deferred coalescing: try to improve performance of free by deferring coalescing until needed. e.g.,

  13. Refinements Separate lists Binary buddy Lea allocator Custom allocators

  14. Lea Allocator An approximate best-fit allocator with different behavior based on object size Small Objects (<64 bytes) allocated by exact-size quicklists Medium Objects (<128K) – coalesce quicklists Large Objects – allocate and free by mmap Generally considered the best allocator known (as of 2000, anyway)

  15. Why programmers use Custom Allocators? Improving runtime performance Reducing memory consumption Improving software engineering (?)

  16. Alternative Memory Management Region (arenas) Reserve memory blocks for program “parts” Deallocate entire regions, not per allocation Garbage collection Programmer allocates but doesn’t free “System” keeps track of memory “pointed to” locations, removes the rest Java

  17. Why Garbage Collect at All? Safety Memory leaks Continued use of freed pointers Simplicity Correctness Programming ease

  18. The Two-Phase Abstraction 1. Detection 2. Reclamation

  19. Liveness and Garbage There is a root set which is defined as live. Anything reachable from a live pointer is also live Everything else is garbage

  20. The Root Set The Root Set Static global and module variables Local Variables Variables on any activation stack(s) Everyone else Anything Reachable From a live value

  21. Reference Counting Each allocated chunk has reference count that shows how many locations point (refer) to this one. Advantages ??? Disadvantages ???

  22. Mark-Sweep Collection Starting from the root set traverse all pointers via depth/breadth first search. Free everything that is not marked.

  23. More Information/Detail • If you wish to know more: •