21
Dynamic Memory Management

Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Embed Size (px)

Citation preview

Page 1: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Dynamic Memory Management

Page 2: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Dynamic Memory Management

Multiprocessor computer environment: • Several programs reside in memory at the same time.• Programs have different memory requirements.

http://saa-www.cs.umass.edu/salsa/NachosFiles/multi.html (multi-prog anim)

OS must •allocate a block of contiguous memory of the right size•reclaim block of memory when program terminates

Basic problem:

Page 3: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Process Memory Map

• Code section– also called text– holds program's instructions– read-only

• Data section – holds the process' global data

and variables.

• Stack – holds local variables – holds arguments to each

function

Page 4: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Memory After Allocating to Some Sample

Programs

Suppose we have 100K units of memory & Programs P1-P5 make memory requests:

P1 10K P4 8KP2 15K P5 20KP3 6K

1 100,00010,000

Free space

25,00031,000

39,00059,000

P1 P2 P3 P4 P5

Page 5: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Memory Allocation Example (Compaction)

1 100,00010,000

Free space

25,00031,000

39,00059,000

P1 Free P3 Free P5

Programs P4 and P2 terminate execution, releasing memory spaceWe now have 3 blocks of free space and 3 blocks of memory that are in use.

What if a program requests a block bigger than any of these 3 free blocks, but smaller than their sum? OS would have to relocate programs to provide enough contiguous free space.http://saa-www.cs.umass.edu/salsa/NachosFiles/multi.html(compaction)

Page 6: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Memory Allocation Example (Compaction)

1 100,00010,000

Free space

25,00031,000

39,00059,000

P1 P2 Free Free P5

Instead, suppose that programs P4 and P3 terminate execution, freeing the memory space that they used.

We now have 3 free blocks, two of which are contiguous.

By recognizing this, the OS can combine the contiguous blocks to form one larger block.

Combining physically adjacent free blocks to form one larger free block is called compacting or coalescing.

Page 7: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Memory Manager

• A library module • handles requests to allocate and

deallocate blocks of memory• obtains extra storage if necessary

Page 8: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Free List

• How does the MM tell which parts of memory are in use (allocated) and which are free?

• The MM is entitled to store anything in free memory, including addresses to allow blocks to be linking.

• MM keeps a free list of blocks of varying sizes that are available for allocation. It is as if each block were variables of this pseudo-type:

Page 9: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Allocating Free Blocks: Strategies

• first fit - Traverse the list to find available block large enough; allocate from first such block found

• best fit - Traverse the list to find the smallest available block that is large enough; allocate from it

• worst fit - Traverse list to find largest available block and allocate from it (if large enough)

• http://www.cse.unsw.edu.au/~cs2011/lectures/memgt/notes.htm(request scenarios)

Page 10: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Allocation Strategies (continued)

Advantages Disadvantages

first fit: No need to traverse entire Small blocks tend to cluster at list except in worst case front of list because search

always begins at front of list

best fit: Small blocks will be Must always search entire distributed around list list.

worst fit: Portions of blocks left Must always search entire list.

in list tend to be larger.

Common disadvantage: Some leftover blocks may be too small to be used.

Page 11: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

First Fit

• The memory manager examines each item in the list until it finds a hole that is large enough for the process

• Unless the process fills this segment (unlikely), the hole is divided into two areas - one for the process and one for the unused memory.

• First fit is a fast algorithm as it examines the list as little as possible. This algorithm is used by UNIX, and DOS (by default). DOS may also be configured to use best fit and last fit (a reversal of first fit).

Page 12: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Best Fit

• The entire list is examined and the smallest adequate hole is allocated to the process (Figure 1).

• Best fit is considerably slower than first fit as it must examine the whole list each time it is called.

• Also, it results in more wasted memory than either first fit or next fit as it tends to fill memory with tiny, useless holes. First fit generally generates larger holes.

Page 13: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Figure 1 The Distinction Between First Fit and Best Fit Policies

Page 14: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Worst Fit

• The entire list is examined and the largest adequate hole is allocated to the process.

• This should result in the hole remaining after

a segment of memory has been allocated to the process being large enough to be useful.

• Simulation has shown this algorithm to be a relatively poor performer.

Page 15: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Which is Best?

• Throughout simulations, the efficiencies of first fit and best fit were generally within 1 to 3 percent of each other.

• There is strong evidence that the relative performance of the two strategies depends on the frequency of requests that are large compared to the average request. When this frequency is high, first fit outperforms best fit because, by preferentially allocating toward one end of memory, first fit encourages large blocks to grow at the other end.

• Bays (1977) also found that the overall performance of first fit may beat that of best fit in many cases, because first fit, by always searching memory from one end, tends to preserve larger holes at the other end. It should also be remembered that first fit has less overheads than best fit,

which must examine the whole list at least once.

Page 16: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Appropriate List Structure for

AllocationFor the allocation strategy, the appropriate structure for theavailable list is:

•linked list -allows for insertion and deletion at arbitrary points in list

•circular - causes smaller blocks to be distributed around the list

•header node - eliminates need to check as special cases:deletion of first block in list, insertion at beginning of list, insertion into empty list

(Give header node a size of 0 so we won’t try to allocate it.)

A singly linked list is sufficient for the allocation strategy. But whatabout freeing (and possibly coalescing) blocks?

Page 17: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Frames / Pages

• Physical memory is divided into fixed-sized blocks called frames .

• Logical memory is divided into equal-sized blocks called pages.

Page 18: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Paging

• Paging permits the physical address space of a process to be non-contiguous, thus allowing a process to be allocated physical memory whereever it is available.

• When a process is to be loaded into memory, its pages are loaded into any available memory frames.

• A page table is maintained for each process. This contains the base address of each page in memory.

• http://meteor.cs.umass.edu/salsa/NachosFiles/multi.html (paging)

Page 19: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Paging (cont.)

• Every address generated by the CPU is divided into two parts - the page number and page offset . The page number is used to index into the page table to get the base address. This is combined with the page offset to determine the physical address.

• Note that with this scheme, there is no external fragmentation because any free frame can be allocated to a process that needs it.

• We may, however, still have internal fragmentation. • If the memory requirements of a process are not exact

multiples of the page size, the last frame allocated will not be completely full.

Page 20: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Page Fault

• An interrupt that occurs when a program requests• data that is not currently in virtual memory. The• interrupt triggers the operating system to fetch the• data from a storage device and load it into RAM

• Invalid Page Fault• A page fault that produces an error. Reasons: • 1. The virtual memory system becomes unstable• due to a shortage of physical memory (RAM) or free disk• 2. The virtual memory area is corrupted by a• misbehaving application. • 3. An application attempts to access data that is• being modified by another running application.

Page 21: Dynamic Memory Management Multiprocessor computer environment: Several programs reside in memory at the same time. Programs have different memory requirements

Paging

• http://webopedia.internet.com/TERM/v/virtual_memory.html

• http://gatsby.lit.tas.edu.au/ostheory/html/memory.htm

• http://meteor.cs.umass.edu/salsa/NachosFiles/multi.html (paging)