Memory Management Techniques
1) Single Contiguous2) Overlays3) Fixed (static) partitions4) Relocation (Dynamic) Partitions5) Paging6) Demand Paging7) Segmented8) Segmented/ Demand Paging
For each technique, observe: Algorithms, Advantages, Disadvantages and Special Requirements
1. Single ContiguousWhile (job is ready) Do
If (JobSize <= MemorySize)Then Begin
Allocate MemoryLoad and Execute JobDeallocate MemoryEnd
Else Error
Advantages:Simplicity
DisadvantagesCPU wasted, Main memory not fully used, Limited job size
II OverlaysPrograms can be sectioned into modules
Not all modules need to be in main memory at the same time
Programmer specifies which modules can overlay each other
Linker inserts commands to invoke the loader when the modules are referenced
The “parent” must stay in memory
A
C
B
D
E
Overlays(Cont)
Advantages:
Reduced memory usage
Disadvantages:
Overlap map must be specified by programmer
Programmer must know memory requirements
Overlapped modules must be completely disjoint
Fixed Partition(1)
Must ensure that a process does not access memory space dedicated to another process
Base Register – Holds the address of the beginning of the partition
Bounds Register – Hold the size of the job
Each relative address is compared to the bounds register and if in range it is added to the base register to produce a physical address
Fixed Partition(2)
Partition
A
A + L
+
A
Virtual Address
L
Bounds Reg
Dynamic Partitions(1)
Consider the following scenario (100K memory):1. Job 1 arrives; size = 22 K2. Job 2 arrives; size = 24 K3. Job 3 arrives; size = 30 K4. Job 4 arrives; size = 10 K5. Job 1 terminates6. Job 3 terminates7. Job 5 terminates
Dynamic Partitions(2)
Lets trace our what happens:
0
100
Where should job 5 be put? What are pros/cons of each placement?
Table Sort Illustration(1)
FREE – 22K
IN USE – 24K
IN USE – 10K
FREE - 30K
FREE 14K
a
c
b
d
e
Table Sort Illustration(2)
First fitStart addr Length A 22 C 30 E 14
Worst fitStart addr Length C 30 A 22 E 14
Best fitStart addr Length E 14 A 22 C 30
Partition Selection Algorithm
Implementation requires a free block table
Sorting table in a particular manner results in a specific selection algorithm
1) First FitSort by location
2) Best FitSort by size (ascending)[don’t break up big blocks)
3) Worst FitSort by size (descending)[break up big blocks]
What if we cannot find a big enough hole for an arriving job?
Suppose a 35K job arrives?
Suppose a 90K job arrives?22K
24K
30K
10K
14K
Free
2
Free
4
Free
CompactionShuffle jobs to create larger contiguous free memory
Do the values of pointers need changing?
Consider the arrival of a 40K job:
Job A 15K
Job B 20K
Job C 7K
Job A 15K
Job B 20K
Job C 7K
Dynamic PartitionsRequires two OS operations:
• Allocation:
Form a partition from a free partition of ample size
• Deallocation:
Return partition to free table and merge where possible
Pros/Cons of Dynamic Partitions
Advantages:
Efficient memory usage
Disadvantages:
Partition Management
Compaction or external fragmentation
Demand Page Management1) Page Map Table (PMT)
Maps page to blockStatus: Main Memory
Secondary MemoryIn Transit
2) Memory Block Table (MBT)Maps block to pageReference BitChange Bit
3) File Map Table (FMT)Maps a job’s pages to secondary memoryPMT for the disk1 FMT/ job1 entry / page
Demand Paging Schematic(1)
3
M
M
S
0
1
2
What does 3 mean?PMTAR
PMT
OS0
1
2
3
4
5
6
7
M = In Memory
S = On secondary storage
I = In transit
Demand Paging Schematic(2)
Disk
0
1
2
FMT
Demand Paging Schematic(3)
OS
J1 P0
J1 P1
0
1
2
3
4
5
6
7
R C
CR
Referenced
Changed
Sharing Pages of Reentrant Code or Data Between Processes
Q. What problem is caused if a shared page frame corresponds to two different page #s? (Hint: consider page replacement)
A. Alias or synonym problem
Address Translation
Steps:
Look up p in TLB
if not found look up p in PMT (must be found)
Replace p by p’ to form PA
Page Replacement
Local VS Global Page Replacement
LocalRequires that each process removes a page from its own set of allocated blocks
GlobalA replacement page may be selected from the set of all blocks
LocalityAt any given time, the locality of a process is the set of pages that are actively being used together
SpacialThere is a high probability that once a location is
referenced, the one after it will be accessed in the near future
EX: Sequential code, array processing
Temporal A referenced location is likely to be accessed again in the near future
EX: Loops. Single data elements
Working Set Theory
W (t,) = The set of pages referenced between times t and t+
The number of elements in W is called the working Set Size
Working set size is an estimation of locality size
A job should not be scheduled unless there is room for its entire working set
Page Replacement Algorithms1) Optimal Replacement
Replace the page which will not be used for the longest period of timeLowest page fault rate of all algorithmsRequires future knowledge
2) FIFOReplace the “oldest” pageA frequently used page may be swapped out
Belady’s Anomaly:For some page replacement algorithms, the page fault rate may increase as the number of blocks increase
Page Trace: 4 3 2 1 4 3 5 4 3 2 1 5
4 3 2 1 4 3 5 4 3 2 1 5
4 3 2 1 4 3 5 4 3 2 1 5
0
1
2
0
1
2
3
Replacement Algorithms Continued
3) Least Recently Used (LRU) Uses the recent past as an approximation of the near future Statck algorithm Does NOT suffer from Belady’s Anomaly Hardware/ Overhead intensive
4) LRU Approximation Uses reference bits in the MBT and a static reference pointer(RP) The reference pointer is not reinitialized between calls to LRU Approximation
LRU Approximation Algorithm
Initially: RP 0;Begin
Done := FALSE;While (NOT Done) Do
If (MBT[RP].ref = 1) Then MBT [RP].ref := 0 Else Begin
Return(RP);Done :== TRUE;End;
RP:=RP+1;End;
LRU Approximation Example
Page Trace: 4 3 2 1 4 3 5 4 2 1 5
NOTE: Slightly different from previous example
Page Reference Bit
0
1
2
RP = 0Page Fault Rate =
Page Replacement
Local VS Global page replacement
LocalRequires that each process remove a page from its own set of allocated blocks
Global A replacement page may be selected from the set of all blocks
VIII Segmentation
-An alternative to paging
-Recall variable Size partitioning:
- Program allocation contiguous
- Use first fit, etc
-Segmentation:
- Put subroutines, arrays, etc. into separate segments
- Each segment must occupy contiguous locations, but segments may be scattered throughout memory
-Segmentation = Paging with variable size pages
-Why do this? What problems arise?
VIII SegmentationDivide each process into logical segments (procedures, arrays, etc)
Logical breakdown gives an intuitive structure to main memory
Managing segments:
Segment Map Table ( SMT )
Length Address
1 SMT/job
1 entry/segment