39
Chapter 6: Memory Management

Purpose of memory manager is to administer the use the use of the primary memory. Primary memory and CPU are the fundamental resources used by every

Embed Size (px)

Citation preview

Chapter 6: Memory Management

Purpose of memory manager is to administer the use the use of the primary memory.

Primary memory and CPU are the fundamental resources used by every process.

Basics Requirements that drive memory designs

REQUIREMENT ON PRIMARY MEMORY

•The primary memory access time must be as small as possible. This need influences both software and hardware design

•The primary memory must be as large as possible. Using virtual memory, software and hardware can make the memory appear to be larger than it actually is

•The primary memory must be cost-effective. The cost cannot be more than a small percentage of the total cost of the computer.

The purpose of the memory manager is to allocate primary memory space to

processes to map the process address space into the

allocated portion of the primary memory to minimize access times using a cost-

effective amount of primary memory

3 Functions of Memory Manager

In an environment that supports dynamic memory allocation, the memory manager must keep a record of the usage of each allocatable block of memory.

Memory allocation is the process assigning blocks of memory on request.

There are 3 memory allocation: i. First Fit, Worst Fit, Best Fit ii. Buddy System Iii. Suballocators

Memory allocation

There are three algorithms for searching the list of free (free list) blocks for a specific amount of memory.◦ First Fit ◦ Best Fit ◦ Worst Fit

When recycling free blocks, there is a choice as to where to add the blocks to the free list. Free list kept:

Memory allocation(address)Increasing size(best fit)Decreasing size (worst fit)Increasing time

5

3.2 Variable Partitioning

First Fit : Allocate the first free block that is large enough for the new process.

This is a fast algorithm. Another strategy is first fit, which simply

scans the free list until a large enough hole is found. Despite the name, first-fit is generally better than best-fit because it leads to less fragmentation.

Variation of first fit known ad next fit, continues each search for a suitable block

6

first fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

7

first fit

Initial memory mapping

Initial memory mapping

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

8

first fit

P4 of 3KB arrives

P4 of 3KB arrives

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

9

first fit

P4 of 3KB loaded here

by FIRST FIT

P4 of 3KB loaded here

by FIRST FIT

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

10

first fit

P5 of 15KB arrives

P5 of 15KB arrives

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

P5 15 KB

<FREE> 1 KB

P3 6 KB

<FREE> 4 KB

11

first fit

P5 of 15 KB loaded here

by FIRST FIT

P5 of 15 KB loaded here

by FIRST FIT

Best Fit : Allocate the smallest block among those that are large enough for the new process.

In this method, the OS has to search the entire list, or it can keep it sorted and stop when it hits an entry which has a size larger than the size of new process.

This algorithm produces the smallest left over block. Problem: Requires more time for searching all the list or

sorting it. It leads to the creation of lots of little holes that are

not big enough to satisfy any requests. This situation is called fragmentation, and is a problem for all memory-management strategies, although it is particularly bad for best-fit.

12

Best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

13

best fit

Initial memory mapping

Initial memory mapping

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

14

best fit

P4 of 3KB arrives

P4 of 3KB arrives

15

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

best fit

P4 of 3KB loaded here

by BEST FIT

P4 of 3KB loaded here

by BEST FIT

16

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

best fit

P5 of 15KB arrives

P5 of 15KB arrives

17

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P5 15 KB

<FREE> 1 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

best fit

P5 of 15 KB loaded here

by BEST FIT

P5 of 15 KB loaded here

by BEST FIT

18

Worst Fit : Allocate the largest block among those that are large enough for the new process.

The memory manager places process in the largest block of unallocated memory available.

The ideas is that this placement will create the largest hole after the allocations, thus increasing the possibility that, compared to best fit, another process can use the hole created as a result of external fragmentation.

worst fit

19

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

worst fit

Initial memory mapping

Initial memory mapping

20

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

worst fit

P4 of 3KB arrives

P4 of 3KB arrives

21

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

worst fit

P4 of 3KB Loaded here

by WORST FIT

P4 of 3KB Loaded here

by WORST FIT

22

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

worst fit

No place to load P5 of 15K

No place to load P5 of 15K

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

23

worst fit

No place to load P5 of 15K

No place to load P5 of 15K

Compaction is needed !!Compaction is needed !!

Compaction is a method to overcome the external fragmentation problem.

All free blocks are brought together as one large block of free space.

Compaction requires dynamic relocation. Certainly, compaction has a cost and selection of

an optimal compaction strategy is difficult. One method for compaction is swapping out

those processes that are to be moved within the memory, and swapping them into different memory locations

24

compaction

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

25

compaction

Memory mapping before

compaction

Memory mapping before

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

26

compaction

Swap out P2

Secondarystorage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

27

compaction

Swap in P2

Secondarystorage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

28

compaction

Swap out P4

Secondarystorage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

29

compaction

Swap in P4 with a different starting address

Secondarystorage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

30

compaction

Swap out P3

Secondary

storage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

31

compaction

Swap in P3

Secondary

storage

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

<FREE> 27 KB

32

compaction

Memory mapping after compaction

Memory mapping after compaction

Now P5 of 15KB can be loaded here

Now P5 of 15KB can be loaded here

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

P5 12 KB

<FREE> 12 KB

33

compaction

P5 of 15KB is loaded

P5 of 15KB is loaded

Static relocation: A process may be loaded into memory, each time possibly having a different starting address

Necessary for variable partitioning Dynamic relocation: In addition to static

relocation, the starting address of the process may change while it is already loaded in memory

Necessary for compaction

34

relocation

The allocator will only allocates blocks of certain sizes and has many free lists, one for each permitted size.

The permitted sizes are usually either powers of 2, such that any block except the smallest can be divided into two smaller blocks of permitted sizes.

Advantages: coalescence is cheap because buddy of any free block can be calculates from its address.

See eg 6.2.2

Buddy System

A suballocator obtains large blocks of memory from the system memory manager and allocates the memory to the application in smaller pieces.

Function suballocator: To avoid general inefficiency in the system

memory manager. To take advantage of special knowledge of the

application’s memory requirements To provide memory management services that

the system memory manager does not supply.

Suballocators

1. Swapping To optimize system performance by removing a

process from primary memory. RAM has limited capacity. Virtual memory as

additional of RAM. The area hard disk used for virtual memory is

called swap file. When more RAM space is needed, the OS swaps

out from RAM and moved to virtual memory. The technique of swapping items between

memory and storage called paging.

Memory Manager Strategies

Allow a process to use the CPU when only part of its address space is loaded in the primary memory.

2. Virtual Memory

Dynamic Relocation

Refers to address transformations being done during execution of a program.

Define memory allocation Give three way to allocate memory in our

system. Explain 3 functions that must be performed

by memory manager in order to execute program and data.

List three basic requirements of designing primary memory.

List 2 strategies to manage memory Define what is best fit, worst fit and first fit.

TutorialQuest of Nov2011

Quest of Nov2010

Quest of Nov2011

Quest of Nov2011