Presentation is loading. Please wait.

Presentation is loading. Please wait.

Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)

Similar presentations


Presentation on theme: "Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)"— Presentation transcript:

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

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

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

4 Memory Allocation Strategies Memory Allocation Strategies Explicit vs. Implicit Memory Allocator Explicit vs. Implicit Memory Allocator General purpose vs. custom allocator General purpose vs. custom allocator Software vs. hardware 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 execution-time performance Good space utilization Good space utilization Good locality properties Good locality properties

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

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 Bidirectional Coalescing Boundary tags [Knuth73] Boundary tags [Knuth73] Replicate size/allocated word at bottom of free blocks Replicate size/allocated word at bottom of free blocks Allows us to traverse the “list” backwards, but requires extra space Allows us to traverse the “list” backwards, but requires extra space Important and general technique! Important and general technique!

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

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

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

13 Refinements Separate lists Separate lists Binary buddy Binary buddy Lea allocator Lea allocator Custom allocators Custom allocators

14 Lea Allocator An approximate best-fit allocator with different behavior based on object size An approximate best-fit allocator with different behavior based on object size Small Objects (<64 bytes) allocated by exact-size quicklists Small Objects (<64 bytes) allocated by exact-size quicklists Medium Objects (<128K) – coalesce quicklists Medium Objects (<128K) – coalesce quicklists Large Objects – allocate and free by mmap Large Objects – allocate and free by mmap The best allocator known The best allocator known

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

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

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

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

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

20 The Root Set The Root Set The Root Set Static global and module variables Static global and module variables Local Variables Local Variables Variables on any activation stack(s) Variables on any activation stack(s) Everyone else Everyone else Anything Reachable From a live value 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. Each allocated chunk has reference count that shows how many locations point (refer) to this one. Advantages ??? Advantages ??? Disadvantages ??? Disadvantages ???

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


Download ppt "Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)"

Similar presentations


Ads by Google