Memory Management

MEMORY
MANAGEMENT
FROM MONOPROGRAMMING TO MULTIPROGRAMMING
WITH SWAPPING
MEMORY HIERARCHY
• Small amount of expensive, fast, volatile cache
• Larger amount of still fast, but slower, volatile ram
• Vast amount of inexpensive, not volatile, but slow
secondary storage
MEMORY MANAGER
• Keeps track of which parts of memory are in use
• Allocate memory to processes when needed
• Deallocate memory when processes complete
MONOPROGRAMMING
• Memory is divided between o/s, process, device drivers
• o/s signals user when process is finished. Waits for
command to load another
• Two drawbacks:
• A single process can run. Time wasted waiting on i/o
• Processes (include o/s) are exposed to malicious or badly written
code
MONOPROGRAMMING
a) Pre-1960 mainframes
b) Embedded systems
c) Early PCs
MULTIPROGRAMMING
RUN PROCESS A WHEN PROCESS B IS WAITING
• Run process A when process B is waiting
• Degree of multiprogramming
• Processes spend up to 80% of time waiting for i/o
• Suppose process spends fraction, t, waiting for i/o
• If N processes, probability that all are waiting is tN
• Then CPU use is 1 - tN
• N is called degree of multiprogramming
EXAMPLE
• Suppose we want 90% CPU use
• .9 = 1 - .8N
• N ~ 10
• Suppose we want 95% CPU use
•
•
•
•
.95 = 1 - .8N
5/100 = (8/100)N
ln 5 – ln 100 = n * (ln 8 – ln 10)
N ~ 13.6
MULTIPROGRAMING
REQUIRES THAT WE
RETHINK MEMORY
a)
16 kb program runs alone.
Finishes
b)
os loads new program.
Finishes
c)
Now suppose os tries to
multiprogram. Instead of
evicting old program, adds
new one above it.
d)
jmp 28 causes an error
e)
Problem: both programs
reference static, physical
addresses
-
FIRST THINGS FIRST
• Multiprogramming requires solution to two problems
• protection: process 1 may not overwrite process 2
• relocation: remap process memory to different parts of physical
memory
• Address Space
• The set of addresses available to a process
• Each process has its own address space
• Key idea: address 28 in process 1 is not the same physical address as
address 28 in process 2
SOLUTION TO BASIC
MULTIPROGRAMMING PROBLEM
BASE AND LIMIT REGISTERS
Simple solution to relocation problem.
•
Base register holds the beginning address
of the process
•
Limit register holds length of the process
•
jmp 28 jumps base register + 28
PROBLEM
• Degree of multiprogramming limited by the amount of memory
• Windows/OS X/Linux 50 – 100 processes may be started up at boot
time
• Two General Solutions
• Swapping
• Load each process
• Run it for a little while
• Save it to disk
• Virtual Memory
• Load only parts of processes
SWAPPING (SOLUTION 1 TO
MULTIPROGRAMMING PROBLEM)
•
Memory allocation changes as processes come into memory and leave it.
•
The shaded regions are unused memory
SWAPPING ISSUES
•
Problem 1
•
Swapping creates holes in memory (external fragmentation)
•
Can be relieved through (very expensive) memory compaction
• Problem 2
• Have assumed that programs have a fixed size
• But many programming languages allow for dynamically allocated
memory
• If free store is adjacent to a process, it can be claimed
• If not, the growing process will have to be moved to a hole large
enough to accommodate it or swapped out and suspended.
SIZE ISSUE
a)
allocate extra space at load time
b)
Distinguish between dynamically allocated heap memory and local stack memory
KEEPING TRACK OF GROWING/SHRINKING MEMORY WITH
BIT MAPS AND LINKED LISTS
a)
A part of memory with five processes and three holes. The tickmarks show the memory allocation
units. The shaded regions (0 in the bitmap) are free.
b)
The corresponding bitmap
c)
Linked list memory management
Problems with bit maps
•
Large allocation unit results in holes in the last unit of the process
PROCESS TERMINATES
Is X adjacent to another process?
FINDING A WHOLE LARGE
ENOUGH FOR THE PROCESS
• First fit
• Scan linked list looking for the first whole that is big enough
• Results in fragmentation
• Next fit
• Start search where last search ended to avoid small hole clustering
• Best fit
• Scan all of memory looking for the best fit
• Sounds good, but results in many tiny and useless holes
• Worst fit
• OK. Look for the largest available hole.
• Also produces fragmentation
• There are other possibilities, including a doubly linked list to make merging easier.
• Fragmentation is a problem with all possibilites