Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.

Similar presentations


Presentation on theme: "Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled."— Presentation transcript:

1 Memory Management What if pgm mem > main mem ?

2 Memory Management What if pgm mem > main mem ? Overlays – program controlled

3 Memory Management What if pgm mem > main mem ? Virtual Memory – OS controlled (with architecture help)

4 Memory Management Separate physical, logical address space Page faults Demand paging

5 Memory Access in VM Is access legal ? (seg fault) If page in physical memory, return mem Else – Find free page – Schedule disk operation – Perform disk operation – Update page table – Restart program at offending address

6 Memory Access Time Assume machine characteristics – 200ns clock rate – Main Mem access of 5 cycles  1000ns – 25 milisecond page service time

7 Computing Average Access Time MAT = (1 – p) * 1000 + p * 25,000,000 e.g. assume page fault rate of.001 MAT =.999 * 1000 +.001 * 25,000,000 = 999 + 25,000  26x penalty

8 Your Turn Those were 1998 numbers Assume 2x speedup in clock speed every two years, 2x speedup in disk access time every 4 years What penalty would.001 page fault rate lead to in 2010? What page fault rate would allow MAT of 2x main memory access time.

9 Replacement Policy (Page) FIFO Random Optimal LRU LRU approximation

10 Sample References 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

11 FIFO – 4 pages 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 2 14 372104 2012 120 3 3401 7012340

12 Optimal – 4 pages 7 0 1 2 0 3 0 4 2 3 0 3 2 1 0 1 7 0 1 2 14 2112 17 000 4 02 73330

13 LRU – 4 pages 7 0 1 2 0 3 0 4 2 3 0 3 2 1 0 1 7 0 1 2 13 3114 27 120 3 22 70000

14 Your Turn 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Show FIFO, LRU, Optimal for references above but with 3 pages.

15 Dynamic Memory Allocation (malloc, free) Memory at execution time? – Static area – Runtime stack – Heap Start with large chunk of memory Reuse memory when possible

16 Consider Starting with 160 units of memory do: – Allocate p1 (50 u) – Allocate p2 (30 u) – Allocate p3 (40 u) – Free p2 – Allocate p4 (40 u) – Free p3 – Allocate p5 (60 u) – Free p1 – Allocate p6 (30u)

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

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

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

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

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

22 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 Space in use

23 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

24 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!

25 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

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

27 Key Allocator Policies Placement policy: – First fit, next 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.,

28 Refinements Separate lists Binary buddy Lea allocator Custom allocators

29 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 The best allocator known

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

31 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

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

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

34 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

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

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

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


Download ppt "Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled."

Similar presentations


Ads by Google