59
Memory Management Memory Management

Memory Management. Managing Memory … The Simplest Case The O/S User Program 0 0xFFF … * Early PCs and Mainframes * Embedded Systems One user program at

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Memory ManagementMemory ManagementMemory ManagementMemory Management

Managing Memory … The Simplest Case

The O/S

User Program

0

0xFFF …

* Early PCs and Mainframes* Embedded Systems

One user program at a time.

The logical address space is the same as the physical address space.

But in most modern But in most modern systems …systems …

But in most modern But in most modern systems …systems …

Modern memory managers subdivide memory to accommodate multiple processes.

Memory needs to be allocated efficiently to pack as many processes into memory as possible

When required a process should be able to have exclusive use of a block of memory, or permit sharing of the memory by multiple processes.

Primary memory is abstracted so that a program perceives that the memory allocated to it is a large array of contiguously addressed bytes (but it usually isn’t).

Modern memory managers subdivide memory to accommodate multiple processes.

Memory needs to be allocated efficiently to pack as many processes into memory as possible

When required a process should be able to have exclusive use of a block of memory, or permit sharing of the memory by multiple processes.

Primary memory is abstracted so that a program perceives that the memory allocated to it is a large array of contiguously addressed bytes (but it usually isn’t).

* Relocation* Protection* Sharing* Physical Organization

Memory Managers: Four ConcernsMemory Managers: Four Concerns

RelocationRelocationRelocationRelocation

The Programmer does not know where the The Programmer does not know where the program will be placed in memory when it is program will be placed in memory when it is executed. While the program is executing, it may executed. While the program is executing, it may be swapped to disk and returned to main memory be swapped to disk and returned to main memory at a different location (relocated). Memory at a different location (relocated). Memory references in the code must be translated to references in the code must be translated to actual physical memory addressactual physical memory address

Process’s logicaladdress space

Physical addressspace

ProtectionProtectionProtectionProtection

With multiple processes in memory and running simultaneously” the system must protect one process from referencing memory locations in another process. This is more of a hardware responsibility than it is an operating system responsibility.

Loading and Linking a ProgramLoading and Linking a Program

Source Compiler

Relocatable Object Module

Relocatable Object Modules

Linker

Load Module

Program SegmentData SegmentStack Segment

Library Modules

All references to data orfunctions are resolved…

Loader

Load Module

Process ImageIn Main Memory

Load Module

Process ImageIn Main Memory

Absolute LoaderAbsolute Loader

program

data

program

data

0 0

no changes in addresses

Load Module

Process ImageIn Main Memory

Static BindingStatic Binding

program

dataprogram

data

0

All addresses in thecode segment arerelative to 0

Add the offset to everyaddress as the code isloaded.

Offset = 1000

Jump 400

Jump 1400

1000

Load Module

Process ImageIn Main Memory

Dynamic Run-time BindingDynamic Run-time Binding

program

dataprogram

data

0

All addresses in thecode segment arerelative to 0

Offset = 1000

Jump 400

Jump 400

Addresses are maintained inrelative format

Address translationtakes place on thefly at run time.

program

data

Base Register

Limit Register

jump, 400

Adder

Comparatorabsolute address

interrupt

1000

relative address400

1400

segment error!

A Code ExampleA Code Example

. . .

static int gVar;

. . . int function_a(int arg) { . . .

gVar = 7; libFunction(gVar); . . .}

static variables are stored in the data segment.

generated code will be stored in the code segment.

libFunction( ) is defined in an external module. At compile time we don’t know the address of its entry point

A Code Example

. . .

static int gVar;

. . . int function_a(int arg) { . . .

gVar = 7; libFunction(gVar); . . .}

0000 . . . . . . 0008 entry function_a . . . 0220 load r1, 7 0224 store r1, 0036 0228 push 0036 0228 call libFunction . . .

0400 External Reference Table . . . 0404 “libFunction” ???? . . . 0500 External Definition Table . . . 0540 “function_a” 0008 . . . 0600 Symbol Table . . . 0799 End of Code Segment

. . . 0036 [space for gVar] . . . 0049 End of Data segment

Code S

eg

ment

Data

Seg

relative addresses

compile

0000 . . . . . . 0008 entry function_a . . . 0220 load r1, 7 0224 store r1, 0036 0228 push 0036 0228 call libFunction . . .

0400 External Reference Table . . . 0404 “libFunction” ???? . . . 0500 External Definition Table . . . 0540 “function_a” 0008 . . . 0600 Symbol Table . . . 0799 (end of code segment)

. . . 0036 [space for gVar] . . . 0049 (end of data segment)

libFunction

0000 (other modules) . . . 1008 entry function_a . . . 1220 load r1, 7 1224 store r1, 0136 1228 push 1036 1232 call 2334 . . . 1399 (end of function_a) . . . (other modules) 2334 entry libFunction . . . 2999 (end of code segment)

. . .0136 [space for gVar]. . . 1000 (end of data segment)

relative addresses

Code S

egm

ent

Data

seg

link

Object File

Contains an external definition tableIndicating the relative entry point

0000 (other modules) . . . 1008 entry function_a . . . 1220 load r1, 7 1224 store r1, 0136 1228 push 0136 1232 call 2334 . . . 1399 (end of function_a) . . . (other modules) 2334 entry libFunction . . . 2999 (end of code segment)

. . . 0136 [space for gVar] . . . 1000 (end of data segment)

0000 (other modules) . . . 5008 entry function_a . . . 5220 load r1, 7 5224 store r1, 7136 5228 push 7136 5232 call 6334 . . . 5399 (end of function_a) . . . (other modules) 6334 entry libFunction . . . 6999 (end of code segment)

. . . 7136 [space for gVar] . . . 8000 (end of data segment)

real addresses

staticbind

loaderLoad Module

SharingSharingSharingSharing

Multiple processes (fork) running the same executable

Shared memory

Physical OrganizationPhysical OrganizationPhysical OrganizationPhysical Organization

The flow of information between the various“levels” of memory.

Registers

Cache

RAM

Disk

Optical, Tape, etc

fast but expensive

cheap but slow

1 machine cycle

Memory AllocationMemory Allocation

Before an address space can be bound to physical addresses, the memory manager must allocate the space in real memory where the address space will be mapped to. There are a number of schemes to do memory allocation.

Fixed PartitioningFixed PartitioningFixed PartitioningFixed PartitioningEqual-size fixed partitions

any process whose size is less than or equal to the partition size can be loaded into an available partition

if all partitions are full, the operating system can swap a process out of a partition

a program may not fit in a partition. Then the programmer must design the program with overlays

Equal-size fixed partitions

any process whose size is less than or equal to the partition size can be loaded into an available partition

if all partitions are full, the operating system can swap a process out of a partition

a program may not fit in a partition. Then the programmer must design the program with overlays

Fixed PartitioningFixed PartitioningFixed PartitioningFixed Partitioning

Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation.

But . . . It’s easy to implement.

Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation.

But . . . It’s easy to implement.

Placement Algorithm with Fixed Placement Algorithm with Fixed Size PartitionsSize Partitions

Placement Algorithm with Fixed Placement Algorithm with Fixed Size PartitionsSize Partitions

Equal-size partitionsbecause all partitions are of equal size, it does not matter which partition is used Placement is trivial.

Equal-size partitionsbecause all partitions are of equal size, it does not matter which partition is used Placement is trivial.

Fixed Partition with Fixed Partition with Different SizesDifferent Sizes

Fixed Partition with Fixed Partition with Different SizesDifferent Sizes

Example is OS/360 MFT. The operator fixed the partition sizes at system start up.

Two options: * Separate Input Queues * Single Input Queue

Multiple Input QueuesMultiple Input QueuesMultiple Input QueuesMultiple Input Queues

O/S0

100K

200K

400K

700K

800K

Partition 1

Partition 2

Partition 3

Partition 4

Jobs are put into the queuefor the smallest partition bigenough to hold them.

Disadvantage?

Memory can go unused,even though there arejobs waiting to run thatwould fit.

Single Input QueueSingle Input QueueSingle Input QueueSingle Input Queue

O/S0

100K

200K

400K

700K

800K

Partition 1

Partition 2

Partition 3

Partition 4 When a partition becomes freepick the first job on the queuethat fits.

Disadvantage?

Small jobs can be put intomuch larger partitions thanthey need, wasting memoryspace.

Single Input QueueSingle Input QueueSingle Input QueueSingle Input Queue

O/S0

100K

200K

400K

700K

800K

Partition 1

Partition 2

Partition 3

Partition 4 Alternative Solution – scan thewhole queue and find the jobthat best fits.

Disadvantage?

Discriminates against small jobs. Starvation.

CPU UtilizationCPU UtilizationCPU UtilizationCPU Utilization

From a probabalistic point of view ….

Suppose that a process spends a fraction p of its timewaiting for I/O to complete. With n processes in memoryat once, the probability that all n processes are waitingfor I/O (in which case the CPU is idle) is p n.

CPU utilization is therefore given by the formula

CPU Utilization = 1 – p n

Consider the case where processes spend 80% of their timewaiting for I/O (not unusual in an interactive end-user systemwhere most time is spent waiting for keystrokes). Notice thatit requires at least 10 processes to be in memory to achievea 90% CPU utilization.

Predicting PerformancePredicting PerformancePredicting PerformancePredicting Performance

Suppose you have a computer that has 32MB of memory and that the operating system uses 16MB. If user programs average 4MBwe can then hold 4 jobs in memory at once. With an 80% averageI/O wait

CPU utilization = 1 – 0.84 = approx 60%

Adding 16MB of memory allows us to have 8 jobs in memory at onceSo

CPU utilization = 1 - .88 = approx 83%

Adding a second 16MB would only increase CPU utilization to 93%

Dynamic PartitioningDynamic PartitioningDynamic PartitioningDynamic Partitioning

Partitions are of variable length and number.A process is allocated exactly as much memory as it requires.

Eventually you get holes in the memory. This is called external fragmentation.

You must use compaction to shift processes so they are contiguous and all free memory is in one block.

Partitions are of variable length and number.A process is allocated exactly as much memory as it requires.

Eventually you get holes in the memory. This is called external fragmentation.

You must use compaction to shift processes so they are contiguous and all free memory is in one block.

O/S 8M

56M

ForExample …

O/S 8M

Process 1 20M

36M

O/S 8M

Process 1 20M

Process 2 14M

18MProcess 3

8M

O/S 8M

Process 1 20M

10M

18MProcess 3

8M

Process 4

4M

O/S 8M

16M

10M

18MProcess 3

8M

Process 4

4M

Process 5

4M

Fragmentation!

Periodically the O/S could do memory compaction – likedisk compaction. Copy all of the blocks of code for loadedprocesses into contiguous memory locations, thus openinglarger un-used blocks of free memory.

The problem is that this is expensive!

A related question: How much memory do you allocate to a process when it is created or swapped in?

In most modern computer languages data can be created dynamically.

The HeapThe HeapThe HeapThe Heap

This may come as a surprise….

Dynamic memory allocation with malloc, or new, does not reallycause system memory to be dynamically allocated to the process.

In most implementations, the linker anticipates the use of dynamicmemory and reserves space to honor such requests. The linkerreserves space for both the process’s run-time stack and it’s heap. Thus a malloc( ) call returns an address within the existing address space reserved for the process.

Only when this space is used up does a system call to the kernel take place to get more memory. The address space may have to be rebound.

Managing Dynamically Managing Dynamically Allocated MemoryAllocated Memory

Managing Dynamically Managing Dynamically Allocated MemoryAllocated Memory

When managing memory dynamically, the operating system must keep track of the free and used blocks of memory.

Common methods used are bitmaps and linked lists.

Linked List AllocationLinked List AllocationLinked List AllocationLinked List Allocation

P 0 5 H 5 3 P 8 6 P 14 4

H 18 2 P 20 6 P 26 3 H 29 3 X

Hole

Starts at

Length

Process

Memory is divided up into somenumber of fixed size allocationunits.

Keep List in order sorted by address

Linked List AllocationLinked List AllocationLinked List AllocationLinked List Allocation

P 0 5 H 5 3 P 8 6 P 14 4

H 18 2 P 20 6 P 26 3 H 29 3 X

When this process ends, just merge this node with the hole next to it (if one exists). We wantcontiguous blocks!

Hole

Starts at

Length

Linked List AllocationLinked List AllocationLinked List AllocationLinked List Allocation

P 0 5 H 5 3 P 8 6 P 14 4

H 18 8 P 26 3 H 29 3 X

When blocks are managed this way, there are several algorithms thatthe O/S can use to find blocks for a new process, or one beingswapped in from disk.

Hole

Starts at

Length

Dynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement AlgorithmsDynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement Algorithms

Best-fit algorithmSearch the entire list and choose the block that is

the smallest that will hold the request. This algorithmis the worst performer overall, since the smallest possible block is found for a process, this algorithm tends to leave

lots of tiny holes that are not useful.

smallest block that process will fit in

tiny hole

Dynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement AlgorithmsDynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement Algorithms

Worst-fit – a variation of best fitThis scheme is like best fit, but when looking for a new block it picks the largest block of unallocated memory.

The idea is that external fragmentation will result in bigger holes, so it is more likely that another block will fit.

Largest block of unallocated memory

big hole

Dynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement AlgorithmsDynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement Algorithms

First-fit algorithmFinds the first block in the list that will fit.

May end up with many process loaded in the front end of memory that must be searched over when trying to

find a free block

Dynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement AlgorithmsDynamic Partitioning Dynamic Partitioning Placement AlgorithmsPlacement Algorithms

Next-fit – a variation of first fitThis scheme is like first fit, but when looking for a new block, it begins its search where it left off the last time. This algorithm actually performs slightly

worse than first fit

SwappingSwapping

Used primarily in timeshared systems with single thread processes.

Optimizes system performance by removing a process from memory when its thread is blocked.

When a process is moved to the ready state, the process manager notifies the memory manager so that the address space can be swapped in again when space is available.

Requires relocation hardware

Swapping can also be used when the memory requirements of the processes running on the system exceed available memory.

System Costs to do SwappingIf a process requires S units of primary storage, and a disk block holds D units of primary storage, then

ceiling(S/D)

disk writes are required to swap the address space to disk. The same number of reads are required to swap the address space back into primary storage.

For example, if a process is using 1000 bytes of memory, anddisk blocks are 256 bytes, then 4 disk writes are required.

Suppose that a process requiring S units of primary storage is blocked for T units of time. The resource wasted because the process stays in memory is

S x T.

What criteria would you use to determine whether or not to swap the process out of primary storage?

Suppose that a process requiring S units of primary storage is blocked for T units of time. The resource wasted because the process stays in memory is

S x T.

What criteria would you use to determine whether or not to swap the process out of primary storage?

How big is S ? If it is small, then the amount of storage made available for other processes to use is minimized, and another process may not fit. Swapping would be wasteful if there is nota process that would fit in the storage made available.

Suppose that a process requiring S units of primary storage is blocked for T units of time. The resource wasted because the process stays in memory is

S x T.

What criteria would you use to determine whether or not to swap the process out of primary storage?

If T is small, then the process will begin competing for primary storage too quickly to make the swap effective.

If T < R, the process will begin requesting memory before it is even completely swapped out (R is the time required to swap).

Suppose that a process requiring S units of primary storage is blocked for T units of time. The resource wasted because the process stays in memory is

S x T.

What criteria would you use to determine whether or not to swap the process out of primary storage?

For swapping to be effective, T must be considerably larger than 2R for every process that the memory manager chooses to swap out, and S must be large enough for other processes to execute.

S is known. Can T be predicted?

S is known. Can T be predicted?

When a process is blocked on a slow I/O device, the memory manager can estimate a lower bound.

What about when a process is blocked by a semaphore operation?

Example Test Questions

A memory manager for a variable sized region strategy has afree list of memory blocks of the following sizes:

600, 400, 1000, 2200, 1600, 2500, 1050

Which block will be selected to honor a request for 1603 bytesUsing a best-fit policy? 2200

Which block will be selected to honor a request for 949 bytesUsing a best-fit policy? 1000

Which block will be selected to honor a request for 1603 bytesUsing a worst-fit policy? 2500

If you were designing an operating system, and had to determine the best way to sort the free block list, how would you sort it for each of the following policies, and why?

Best-fit Smallest to largest free block

Worst-fit Largest to smallest free block