Sie sind auf Seite 1von 11

Memory Management Basics

Basic Memory Management Concepts


Address spaces

Physical address space — The address space MAXsys


supported by the hardware
¾ Starting at address 0
0, going to address MAXsys
MAXprog
Logical/virtual address space — A process’s
view of its own memory Program
¾ Starting at address 0, going to address MAXprog P
0

But where do addresses come from?


MOV r0, @0xfffa620e
0
2
Which is bigger, physical or virtual address
space?
¾ A. Physical address space
¾ B. Virtual address space
¾ C. It depends on the system.

Basic Concepts
Address generation

The compilation pipeline

0 1000
Library Library
Routines Routines
0 100 1100
prog P P:
: : :
: :
push ... : :
: push ...
inc SP, 4 : :
foo() inc SP, x jmp 175 jmp 1175
jmp 75
: jmp _foo : :
:
: : ... ...
75 ... 175 1175
end P foo: ...

Compilation Assembly Linking Loading

4
Program Relocation

Program issues virtual addresses


Machine has physical addresses.
If virtual == physical, then how can we have multiple
programs resident concurrently?
Instead, relocate virtual addresses to physical at run
time.
¾ While we are relocating, also bounds check addresses for
safety.
I can relocate that program (safely) in two registers…

Basic Concepts (Cont’d.)


Address Translation

MAXsys
MEMORY
EXCEPTION

Logical no Physical
Addresses Addresses 1500 Program
CPU ≤ yes + P’s
physical
address
1000 space
Instructions 500 1000
MAXprog
Limit Base
Program Register Register
P’s
logical
address
space 0
0
6
With base and bounds registers, the OS needs a hole
in physical memory at least as big as the process.
¾AA. True
T
¾ B. False

Evaluating Dynamic Allocation Techniques


The fragmentation problem

External fragmentation
¾ Unused memory between units of MAX
allocation
¾ E.g, two fixed tables for 2, but a party of 4

Internal fragmentation
¾ Unused memory within a unit of allocation
¾ E.g., a party of 3 at
Program
a table for 4 Q’s
Program Code PAS
(“text”)

Execution
Data Stack

Execution Stack
Program
0 R’s PAS
8
Simple Memory Management Schemes
Dynamic allocation of partitions

MAX Program
Simple approach:
¾ Allocate a partition when a process is admitted P1
into the system
¾ Allocate a contiguous memory partition to the
process Program
P2
OS keeps track of...
Full-blocks P5
Empty-blocks (“holes”)

Allocation strategies Program


f
First-fit P3
Best-fit
Worst-fit
Program
P4
0
9

First Fit Allocation

To allocate n bytes, use the


first available free block such 1K bytes
g
that the block size is larger
than n.

2K bytes 2K bytes

To allocate 400 bytes,


we use the 1st free block 500 bytes 500 bytes
available

10
Rationale & Implementation

Simplicity of implementation

Requires:
q
¾ Free block list sorted by address
¾ Allocation requires a search for a suitable partition
¾ De-allocation requires a check to see if the freed partition could be
merged with adjacent free partitions (if any)

Advantages Disadvantages
‹ Simple ‹ Slow allocation
‹ Tends to produce larger ‹ External fragmentation
free blocks toward the end
of the address space

11

Best Fit Allocation

To allocate n bytes, use the


smallest available free block 1K bytes 1K bytes
such that the block size is
larger than n.

2K bytes 2K bytes

To allocate 400 bytes,


we use the 3rd free block 500 bytes
available (smallest)

12
Rationale & Implementation

To avoid fragmenting big free blocks

To minimize the size of external fragments produced

Requires:
¾ Free block list sorted by size
¾ Allocation requires search for a suitable partition
¾ De-allocation requires search + merge with adjacent free partitions,
if any

Advantages Disadvantages
‹ Works well when most ‹ External fragmentation
allocations are of small size ‹ Slow de-allocation
‹ Relatively simple ‹ Tends to produce many
useless tiny fragments (not
really great)
Doug Lea’s malloc “In most ways this malloc is a best-fit
allocator”
13

Worst Fit Allocation

To allocate n bytes, use the


largest available free block 1K bytes 1K bytes
such that the block size is
larger than n.

2K bytes

To allocate 400 bytes,


we use the 2nd free block
500 bytes
available (largest)

14
Rationale & Implementation

To avoid having too many tiny fragments

Requires:
¾ Free block list sorted by size
¾ Allocation is fast (get the largest partition)
¾ De-allocation requires merge with adjacent free partitions, if any,
and then adjusting the free block list

Advantages Disadvantages
‹ Works
W k best
b t if allocations
ll ti ‹ Slow d
Sl de-allocation
ll ti
are of medium sizes ‹ External fragmentation
‹ Tends to break large free
blocks such that large
partitions cannot be allocated

15

Allocation strategies

First fit, best fit and worst fit all suffer from
external fragmentation.
¾ A. True
¾ B. False

16
Dynamic Allocation of Partitions
Eliminating Fragmentation

MAX Program
Compaction
¾ Relocate programs to coalesce holes
P1

Swapping Program
¾ Preempt processes & reclaim their memory P2

g
Program
Ready Running
P3

ready
queue
? Waiting
Program
Suspended
P4
suspended
queue semaphore/condition queues 0
17

Memory Management
Sharing Between Processes

2n-1
Heap
Schemes so far have considered only a single
dd
address space per process
¾ A single name space per process Run-Time
Program
¾ No sharing P’s
Stack
VAS

Program
How can one share code and data between
Data
programs without paging?
Program
Text
0
Program P’s
VAS 18
Multiple Name Spaces
Example — Protection/Fault isolation & sharing

2n-1 2n -1
4

Heapp Heapp
n3
2 -1 0
Run-Time Run-Time
Stack Stack
2n -12
0
Program Program 2n6-1
Data Data Libraries
2n -1 1 0
Program Program 0 2n5-1
Text Text User
0 0 Code
0
19

Supporting Multiple Name Spaces


Segmentation

New concept: A segment — a memory “object”


¾ A virtual address space

A process now addresses objects —a pair (s, addr)


¾ s — segment number
¾ addr — an offset within an object
™ Don’t know size of object, so 32 bits for offset?

n2 0 n1 0 n 0

s addr
s addr

Segment + Address register scheme Single address scheme

20
Implementing Segmentation
Base + Limit register scheme Physical
Memory

Program Add a segment table containing base &


P limit register values

CPU MEMORY 1500


EXCEPTION Program
s o P’s
no Segment
n 0 32 0
1000
Logical
≤ yes +
Addresses
Limit Base
Register 500 1000 Register
base limit

s
STBR 0
Segment Table 21

Memory Management Basics


Are We Done?

Segmentation allows sharing

… but leads to p
poor memory
y utilization
¾ We might not use much of a large segment, but we must keep the
whole thing in memory (bad memory utilization).
¾ Suffers from external fragmentation
¾ Allocation/deallocation of arbitrary size segments is complex

How can we improve memory management?


¾ Paging

22

Das könnte Ihnen auch gefallen