Unit 7
Unit 7
Memory Management
Why memory needs to be
managed?
Before a program can be executed, it must be
loaded into memory.
In a multi-programming environment, programs are
loaded into different portions of the memory.
Discrepancies occurred, especially when the codes
are referring to are logical addresses, and their
actual physical addresses in the physical memory
are different.
Physical address vs. Logical address
Logical address :
The address that a program perceived after it was
compiled.
Usually starts from 0 and increases contiguously
onward till the end of the program.
Physical address vs. Logical address
Physical address :
The address that the CPU is referring.
Usually starts from 0 and increase contiguously
onward till the extreme of the available memory
module.
Different programs can be resided within the
available memory chips. Hence, the starting
addresses (physical) usually are not 0.
Discrepancy example
Physical memory
Program X Starting address 0
Used
Starting address 0
Program X starts
at, say 2712 Available
Jump to 4000
Used
Jump to 6712
Available
Address 4000
After loading, the
target of this jump
should be 2712+4000, Used
which is 6712. .
Obviously, this requires some .
kind of management .
To where in the memory a
program is loaded?
How to determine where a program should
be loaded to, within the memory?
Some suggestions:
First fit
Best fit
Worst fit
There exists some other schemes, of course.
First fit
Physical memory
Program is loaded into the FIRST Used
Program I Available(100K
)
(35K)
Used
Available(40K)
Used
Available(60K)
Worst fit
Physical memory
Program is loaded into the Biggest Used
(35K) Used
(35K) Used
Leaving a
Available(40K)
5K hole
This hole has a good
Used
chance that it cannot
be used anymore. Available(60K)
Analysis of the 3 schemes
+ s
p
s s’ s’
+ d
p
s’
p’
FIFO (First In First Out) – replace the page that was brought
in at the earliest time. (simple to implement)
OPT (Optimal) – replace the page that won’t be used in the
nearest future. (In reality, it is hard to figure out when a page
will be needed)
LRU (Least Recently Used) – replace the page that has not
been used for the longest time. (A good algorithm but need
hardware support to keep track of the usage history)
LFU (Least Frequently Used) – replace the page that was
used rarely.
Page replacement algorithm