MEMORY MANAGEMENT
MEMORY MANAGEMENT
Memory management
• We have seen how CPU can be shared by a set
of processes
– Improve system performance
– Process management
• Need to keep several process in memory
– Share memory
• Learn various techniques to manage memory
– Hardware dependent
Memory management
What are we going to learn?
• Basic Memory Management: logical vs.
physical address space, protection, contiguous
memory allocation, paging, segmentation,
segmentation with paging.
1. Protect OS
2. Protect user processes
Base and Limit Registers
• A pair of base and limit registers define
the logical address space
Hardware Address Protection with Base and Limit Registers
14000
Relocatable
code
Hardware Support for Relocation
and Limit Registers
• Relocation registers used to protect user processes from each other, and from changing
operating-system code and data
• Relocation register contains value of smallest physical address
• Limit register contains range of logical addresses – each logical address must be less
than the limit register
• Context switch
• MMU maps logical address dynamically
OS OS OS OS OS
• Best-fit: Allocate the smallest hole that is big enough; must search
entire list, unless ordered by size
– Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search entire list
– Produces the largest leftover hole
Fragmentation
• Processes loaded and removed from memory
– Memory is broken into little pieces
offset
page
page number page offset
p d
m-n n
– For given logical address space 2m and page size 2n
Paging Hardware
Paging Example
Logical address 0
(0*4+0)
Logical address = 16 Physical address:
Page size=4 (5*4+0)=20
Physical memory=32
Logical address 3
(0*4+3)
Physical address:
(5*4+0)=23
Logical address 4
User’s view (1*4+0)
Physical address:
(6*4+0)=24
Run time address binding
Logical address 13
(3*4+1)
Physical address:
(2*4+1)
n=2 and m=4 32-byte
memory and 4-byte pages
Paging
• External fragmentation??
• Calculating internal fragmentation
– Page size = 2,048 bytes
– Process size = 72,766 bytes
– 35 pages + 1,086 bytes
– Internal fragmentation of 2,048 - 1,086 = 962 bytes
• So small frame sizes desirable?
– But increases the page table size
– Poor disk I/O
– Page sizes growing over time
• Solaris supports two page sizes – 8 KB and 4 MB
• User’s view and physical memory now very different
– user view=> process contains in single contiguous memory space
• By implementation process can only access its own memory
– protection
Page table entries
• Each page table entry 4 bytes (32 bits) long
• Each entry can point to 232 page frames
• If each frame is 4 KB
• The system can address 244 bytes (16TB) of
physical memory
Use’s view
System’s view
RAM RAM
Before allocation After allocation
Executable file and virtual address
Symbol table
Name address
SQR 0
a.out
SUM 4 Virtual address
space
Implementation of Page Table
• For each process, Page table is kept in main memory
• Page-table base register (PTBR) points to the page table
• Page-table length register (PTLR) indicates size of the
page table
• In this scheme every data/instruction access requires two
memory accesses
– One for the page table and one for the data / instruction
• The two memory access problem can be solved by the
use of a special fast-lookup hardware cache called
associative memory or translation look-aside buffers
(TLBs)
Associative memory
Associative Memory
• Associative memory – parallel search
Page # Frame #
• On a TLB miss, value is loaded into the TLB for faster access next time
– Replacement policies must be considered (LRU)
– Some entries can be wired down for permanent fast access
• Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely
identifies each process (PID) to provide address-space protection for that process
– Otherwise need to flush at every context switch
Paging Hardware With TLB
Effective Access Time
• Associative Lookup = time unit
– Can be < 10% of memory access time
• Hit ratio =
– Hit ratio – percentage of times that a page number is found in the
associative registers; ratio related to size of TLB
• Consider = 80%, = 20ns for TLB search, 100ns for memory access
• Consider = 80%, = 20ns for TLB search, 100ns for memory access
– EAT = 0.80 x 120 + 0.20 x 220 = 140ns
• Consider better hit ratio -> = 98%, = 20ns for TLB search, 100ns for
memory access
– EAT = 0.98 x 120 + 0.02 x 220 = 122ns
Memory Protection
• Memory protection implemented by associating protection bit
with each frame to indicate if read-only or read-write access is
allowed
– Can also add more bits to indicate page execute-only, and so on
10240
ptr
Shared
memory
Structure of the Page Table
• Memory requirement for page table can get huge using straight-
forward methods
– Consider a 32-bit logical address space as on modern computers
– Page size of 4 KB (212)
– Page table would have 1 million entries 220 (232 / 212)
– If each entry is 4 bytes -> 4 MB of physical address space / memory for
page table alone
• That amount of memory used to cost a lot
• Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
d
p1
p2
Pentium II
Address-Translation Scheme
Pentium II
64-bit Logical Address Space
• Even two-level paging scheme not sufficient
• If page size is 4 KB (212)
– Then page table has 252 entries
– If two level scheme, inner page tables could be 210 4-byte entries
– Address would look like
inner page
outer page page offset
p1 p2 d
42 10 12
SPARC (32 bits), Motorola 68030 support three and four level paging respectively
Hashed Page Tables
• Common in virtual address spaces > 32 bits
• Each element contains (1) the page number (2) the value of the
mapped page frame (3) a pointer to the next element
Address space ID
Swapping
A process, or a portion of a process, can be swapped temporarily out of memory to a
backing store and then brought back into memory for continued execution
Expensive
Advantages :
Allows physical memory to be oversubscribed, so that the system can accommodate more
processes than there is actual physical memory to store them.
Idle or mostly idle processes are good candidates for swapping; any memory that has been
allocated to these inactive processes can then be dedicated to active processes.
Medium Term Scheduling
disk Swap Space
Swapper
Swapping with Paging
• When we want to
execute a process, swap
in
• Pager
Swapping with Paging
Segmentation
• Memory-management scheme that supports user view of
memory
• A program is a collection of segments
– A segment is a logical unit such as:
main program Compiler generates the
procedure segments
Loader assign the seg#
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
Executable file and virtual address
Symbol table
Name address
SQR 0
a.out
SUM 4 Virtual address
space
User’s View of a Program
User specifies each address
by two quantities
(a) Segment name
(b) Segment offset
4
1
3 2
4
Logical
address
space user space physical memory space
• Long term scheduler finds and allocates memory for all segments of a program
• Variable size partition scheme
Memory image
Executable file and virtual address
Symbol table
Name address
SQR 0
a.out
SUM 4 Virtual address
space
Paging view
0 Load 0
4 ADD 4
Segmentation view
<CODE, 0> Load <ST,0>
<CODE, 2> ADD <ST,4>
Segmentation Architecture
• Logical address consists of a two tuple:
<segment-number, offset>
• Segment table – maps two-dimensional logical address
to physical address;
• Each table entry has:
– base – contains the starting physical address where the
segments reside in memory
– limit – specifies the length of the segment
• Segment-table base register (STBR) points to the
segment table’s location in memory
• Segment-table length register (STLR) indicates number
of segments used by a program;
segment number s is legal if s < STLR
Example of Segmentation
Segmentation Hardware
Example of Segmentation
Segmentation Architecture
• Protection
• Protection bits associated with segments
– With each entry in segment table associate:
• validation bit = 0 illegal segment
• read/write/execute privileges
• Code sharing occurs at segment level
• Since segments vary in length, memory allocation is
a dynamic storage-allocation problem
– Long term scheduler
– First fit, best fit etc
• Fragmentation
Segmentation with Paging
Key idea:
Segments are splitted into multiple pages
max1 max2
Page table=220
entries
Example: The Intel Pentium
Segment #3
Logical address of a
segment
Page table of
Segment #3
Intel Pentium Segmentation
Pentium Paging Architecture
Pages maybe on
disk, valid/invalid
bit
Problem 1
Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte
page size and page table entries of 4 bytes each. Compute the size of the page table in
the system in megabytes
Consider a machine with 64 MB physical memory and a 32-bit virtual address space. If
the page size is 4 KB, what is the approximate size of the page table?
Problem 2
Large virtual
space
Small memory
Demand Paging
• Bring a page into memory only when it is needed
….
ii Disk
address
• During address translation, if valid–invalid bit in page table entry
is i page fault
Page Fault
• If the page in not in memory, first reference to that page will trap to
operating system:
page fault
• Page-replacement algorithm
– Which frames to replace
– Want lowest page-fault rate
• Page fault
– No free frame
– Terminate? swap out? replace the page?
• Page replacement – find some page in memory, not really in use, page it out
– Performance – want an algorithm which will result in minimum number of page faults
P2
Need For Page Replacement
P1
P2
PC
Basic Page Replacement
1. Find the location of the desired page on disk
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing Effective
memory access time
Page Replacement
5
5 6 5
6
6
Page Replacement
5 5
6
6
Page Replacement
• Modify (dirty) bit to reduce overhead of page
transfers
– only modified pages are written to disk
Performance of Demand Paging
Demand paging affects the performance of the computer systems
Associates a time with each frame when the page was brought
into memory
Limitation:
A variable is initialized early and constantly used
FIFO Page Replacement
3 frames (3 pages can be in memory at a time)
15 page faults
9 page faults
Least Recently Used (LRU)
Algorithm
• Use past knowledge rather than future
– Past is the proxy of future
• Replace page that has not been used in the most of the
time
• Associate time of last use with each page
Time of
Clock Use
CPU
0
1
address
2
3
LRU Algorithm-Implementation
• Stack implementation
• Keep a stack of page numbers in a double linked list form:
• Page referenced:
• move it to the top
• Victim page is the bottom page
LRU Approximation Algorithms
• LRU needs special hardware and still slow
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
• Additional reference bit algorithm
– Record the reference bits in regular interval
– Keep a 8 bit string for each page in memory
– At regular interval, timer copies the reference bit
to the high order bit (MSB) of the string.
– Shift the other bits right side by one bit
Reset
reference
LRU Approximation Algorithms
Period Period Period
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
LRU Approximation Algorithms
• LRU needs special hardware and still slow
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
• Additional reference bit algorithm
• Second-chance algorithm
– Generally FIFO, plus hardware-provided reference bit
– Clock replacement
– If page to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
– set reference bit 0, leave page in memory (reset the time)
– replace next page, subject to same rules
Second-Chance (clock) Page-Replacement Algorithm
FIFO Example –
Program - 5 pages,
3 frames of Memory allocated
1 2 3 4 1 2 5 1 2 3 4 5
9 1 1 1 4 4 4 5 5 5 5 5 5
faults 2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
FIFO Example –
Program - 5 pages,
4 frames of Memory allocated
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 4 4 4 5 5 5 5 5 5
2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
10 1 1 1 1 1 1 5 5 5 5 4 4
faults 2 2 2 2 2 2 1 1 1 1 5
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3
Belady's Anomaly
# of Page Faults
Number of Frames
cs431-cotter 126
Belady’s Anomaly
• This most unexpected result is known as
Belady’s anomaly – for some page-
replacement algorithms, the page fault rate
may increase as the number of allocated
frames increases
3 6 4 3 9
5 3 6 4 3 In memory
4 frames 6 5 3 4
6
5 5 6
5
Page-Buffering
• Systems commonly keep a pool of free frames.
• When a page fault occurs, a victim frame is chosen as before.
• However, the desired page is read into a free frame from the
pool before the victim is swapped out.
• This procedure allows the process to restart as soon as
possible, without waiting
• When the victim is later swapped out, its frame is added to
the free-frame pool.
Page-Buffering
• Whenever the paging device is idle, a
modified page is selected and is written to
secondary storage.
• Its modify bit is then reset.
• This scheme increases the probability that
a page will be clean when it is selected for
replacement and will not need to be swapped
out.
Page-Buffering
• Another modification is to keep a pool of free
frames
– but to remember which page was in each frame.
• Since the frame contents are not modified when
a frame is swapped to secondary storage,
– the old page can be reused directly from the free-
frame pool if it is needed before that frame is reused.
– No I/O is needed
• When a page fault occurs, we first check whether
the desired page is in the free-frame pool.
Allocation of Frames
• How do we allocate the fixed amount of
memory among various processes?
• Local replacement – each process selects from only its own set of
allocated frames
– More consistent per-process performance
– But possibly under-utilized memory
Page Size