Slide 8 OS Virtual Memory 2025
Slide 8 OS Virtual Memory 2025
Virtual Memory
4
Benefits
Ability to execute a program, partially in memory has many :
• Less number of I/O needed to load or swap program into memory.
• A program no longer be constrained by amount of MM available.
• Each program take less physical memory
– more programs run at same time,
– increase in CPU utilization and throughput.
5
Virtual Memory
Example: Assume
• OS requires 300 MB of memory to store all the running programs.
• However, only 50 MB of physical memory is currently available.
Explanation:
• OS will set up 250 MB of virtual memory
• Use Virtual Memory Manager (VMM) to manage that 250 MB.
• VMM will create a required 250 MB extra memory on hard disk.
• OS will address memory as 300 MB of physical memory, even if only 50
MB is available.
• The job of the VMM to manage 300 MB memory even if just 50 MB of
physical memory space available.
6
Demand Paging
• Demand paging is a memory management technique where only required
pages of a process are loaded into RAM, while the rest remain on disk
(swap space) until needed.
• Key Idea: Instead of loading the entire process into memory at once, the
OS loads pages on demand (when the process requests them).
• Working of Demand Paging:
– When a process starts, only the necessary pages (like main program
instructions) are loaded into RAM.
– If a process tries to access a page not in RAM, a page fault occurs.
– The OS loads the missing page from secondary storage (disk) into RAM.
– The page table is updated to reflect the new mapping of virtual pages to
physical frames.
– The process resumes execution after the page is available in RAM.
7
Demand Paging
• Example: Let’s assume a program has 8 pages, but only 4 frames are
available in RAM. Initially, only a few pages are loaded:
Virtual Page Physical Frame
0 5
1 3
2 - (Page Fault)
3 9
• When the CPU tries to access page 2, a page fault occurs, and the OS
loads page 2 from disk into RAM.
8
Demand Paging
• Advantages:
– Reduces memory usage by loading only required pages.
– Enables execution of large programs on limited RAM.
– Improves CPU utilization by allowing more processes to run simultaneously.
• Disadvantages:
– Page faults increase latency since loading from disk is slow.
– Thrashing can occur if too many page faults happen frequently.
9
Demand Paging
• When a process will run, bring only a few necessary pages into memory.
• During running the process, bring a page into memory only when it is
needed (lazy swapping)
– Less I/O needed
– Less memory needed
– Faster response
– Support more processes/users
• Page is needed
– If memory resident use the reference to page from page table
– If not in memory Page fault trap
10
Demand Paging
• Hardware support is required to distinguish between
– Pages are in memory and
– Pages are on the disk
• valid-invalid bit scheme: Scheme to implement
– valid and invalid pages can be checked by checking the bit.
• Marking a page will have no effect if the process never attempts to access
the page.
• Process execution proceeds normally when access to a page is in
memory.
11
Valid-Invalid Bit
• Page table entry is associated with a Frame # valid-
valid–invalid bit invalid bit
– v in-memory i.e. memory resident, v
v
– i not-in-memory v
• Initially valid–invalid bit is set to i on all entries v
i
….
• Example of a page table snapshot: i
i
• During address translation, if valid–invalid bit in
Page Table
page table entry is i page fault
12
Use of Page Table
13
Page Fault
• A page fault occurs when a process tries to access a page that is not
currently in RAM.
– first reference to each page always a page fault
• The OS then fetch the required page from disk and load it into RAM
before execution continues.
• OS decides:
– Invalid reference abort.
– i.e. just not in memory.
14
Handling a Page Fault
15
Steps in Handling a Page Fault
16
Performance of Demand Paging
• Page Fault Rate 0 p 1.0
– if p = 0 no page faults
– if p = 1, every reference is a fault
17
Demand Paging: Example
Given:
• Memory access time = 200 nanoseconds
• Average page-fault service time = 8 milliseconds = 8,000,000 ns
• One access out of 1,000 causes a page fault
– page fault rate (p) = 10-3 = 0.001
• Side effect: The same page may be brought in and out of memory
several times.
19
Page Replacement
• Use modify (dirty) bit to reduce overhead of page transfers
– only modified pages are written to disk
20
Page Replacement
• Basic idea:
– if there is a free frame in memory, use it
– if not, select a victim frame
– write the victim out to disk
– read the desired page into the now free frame
– update page tables
– restart the process
21
Page Replacement
22
Page Replacement Algorithm: Basics
23
Reference String
• Reference string is the sequence of pages being referenced.
• If user has the following sequence of addresses
– 123, 215, 600, 1234, 76, 96
• If the page size is 100, then the reference string is
– 1, 2, 6, 12, 0, 0
24
Page and Frame Replacement Algorithms
26
First-In First-Out (FIFO)
• The First-In-First-Out (FIFO) page replacement algorithm removes the
oldest page from memory when a new page needs to be loaded and no
free frames are available.
27
First-In First-Out (FIFO) Algorithm
Problem: A system uses 3 page frames for storing process pages in main
memory. It uses the First in First out (FIFO) page replacement policy.
Assume that all the page frames are initially empty.
What is the total number of page faults that will occur while processing the
page reference string given below:
– 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
– Also calculate the hit ratio and miss ratio.
28
First-In First-Out (FIFO) Algorithm
30
First-In First-Out (FIFO) Algorithm
Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%
31
First-In First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 4 5
• 3 frames (3 pages can be in memory 2 2 1 3 9 page
at a time per process): faults
3 3 2 4
1 1 5 4
• 4 frames: 2 2 1 5 10 page
faults
3 3 2
4 4 3
• FIFO Replacement manifests Belady’s Anomaly:
– occurs when increasing the number of frames results in more page
faults instead of reducing them
– i.e. more frames less page faults
32
FIFO Illustrating Belady’s Anomaly
33
FIFO Information
Time & Space Complexity:
• Time Complexity = O(1) (Enqueue and Dequeue operations are constant
time).
• Space Complexity = O(n) (Stores page references in memory).
Advantages:
• Simple and easy to implement.
• Fast execution (Queue-based).
Disadvantages:
• Belady’s Anomaly (May increase page faults with more frames).
• Not optimal (May evict important pages).
• Ignores page usage frequency (LRU performs better).
34
Optimal Page Replacement
35
Optimal Algorithm
36
Optimal Algorithm
• A system uses 3 page frames for storing process pages in main memory.
• It uses the Optimal page replacement policy.
• Assume that all the page frames are initially empty.
• What is the total number of page faults that will occur while processing
the page reference string given below:
– 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
• Also calculate the hit ratio and miss ratio.
37
Optimal Algorithm
Advantages:
• Guaranteed minimum page faults.
• No Belady’s Anomaly.
• Best for theoretical comparison of page replacement algorithms.
Disadvantages:
• Not practical (requires future knowledge).
• Can’t be implemented in real OS.
• Only used for performance benchmarking.
39
Least Recently Used (LRU)
• The Least Recently Used (LRU) algorithm replaces the page that has not
been used for the longest time when a new page needs to be loaded into
memory.
• Concept: Pages that haven't been used for the longest time are the least
likely to be needed soon.
• Implementation: Requires tracking of past page accesses.
40
Least Recently Used (LRU)
Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of time
• Associate time of last use with each page
42
Least Recently Used (LRU)
Algorithm
• Total number of references = 10
• Reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
43
LRU Issues
• How to keep track of last page access?
– requires special hardware support
• 2 major solutions
– counters
• hardware clock “ticks” on every memory reference
• the page referenced is marked with this “time”
• the page with the smallest “time” value is replaced
– stack
• keep a stack of references
• on every reference to a page, move it to top of stack
• page at bottom of stack is next one to be replaced
44
LRU Issues
Advantages:
• Better than FIFO (avoids Belady’s Anomaly).
• Close to OPT in real-world performance.
• Widely used in real operating systems.
Disadvantages:
• Extra overhead (tracking order of page accesses).
• Expensive to implement efficiently in hardware.
45
Use Of A Stack to Record The
Most Recent Page References
46
Thrashing
§ Thrashing occurs when a system spends more time swapping pages in and
out of memory (paging) than executing actual processes.
§ This leads to severe performance degradation and
§ can cause the system to become nearly unresponsive.
§ Cause: Excessive page faults due to insufficient memory allocation.
§ Effect: CPU utilization drops significantly as it spends most of its time
handling page faults.
47
Thrashing
Working Set Model (WSM) and Thrashing
§ A process needs a certain number of pages (its working set) to execute
efficiently.
§ If it does not get enough frames, it will constantly trigger page faults,
leading to frequent swapping (thrashing).
§ When total memory demand > available memory ⟶Thrashing occurs.
§ Page fault rate becomes very high, leading to excessive disk I/O.
§ CPU spends more time handling page faults instead of running processes.
Thrashing Scenario
§ The system allocates fewer frames per process than required.
§ Processes keep requesting pages that are not in memory.
§ Frequent page replacements cause pages to be swapped in and out
continuously.
§ CPU utilization drops drastically ⟶ OS may think it needs to load more
48 processes, making the situation worse.
Thrashing
Symptoms of Thrashing
• High page fault rate (sudden spike in page faults per second).
• Slow system performance (even though CPU is active).
• Excessive disk activity (frequent paging/swapping).
• Low CPU utilization (OS spends time handling page faults instead of
running processes).
49
Thrashing
How to Prevent or Control Thrashing?
1. Working Set Model (WSM)
• The idea: Keep the entire working set of pages in memory to minimize
page faults.
– Each process is allocated enough frames to hold its working set.
– If the working set changes dynamically, the OS adjusts frame allocation
accordingly.
50
Thrashing
How to Prevent or Control Thrashing?
3. Page-Fault Frequency (PFF) Algorithm
• The OS dynamically adjusts frame allocation based on the observed page
fault rate.
• If page faults increase, allocate more frames.
• If page faults decrease, deallocate extra frames to free memory.
52
Thrashing: Example
Assume that 3 processes (P1, P2, P3) and each requires 5 frames to run efficiently.
Case 1: Enough Frames (No Thrashing)
• Available Frames = 15
• Each process gets 5 frames.
• Page faults occur occasionally, but processes run smoothly.
Case 2: Memory Overload (Thrashing)
• Available Frames = 9 (insufficient)
• Each process gets only 3 frames instead of 5.
• Frequent page faults occur, and processes keep swapping pages.
• CPU spends most of its time handling page faults instead of executing
processes.
Solution?
• Reduce the number of processes.
• Allocate enough frames per process.
53 • Use page-fault frequency (PFF) to monitor memory demand.
Thrashing
Thrashing results in severe performance problems:
• Low CPU utilization increases the degree of multiprogramming by
introducing a new process to the system.
• CPU utilization is plotted against the degree of multiprogramming.
• As the degree of multiprogramming increases, CPU utilization also increases.
• If the degree of multiprogramming is increased further, thrashing sets in and
.
• At this point, to increase CPU utilization and to stop thrashing, decrease
the degree of multiprogramming.
54
Solution to Thrashing
• How do we fix thrashing?
– Working Set Model (Based on Locality Model)
– Page Fault Frequency
55
Locality Model
• As a process executes, it moves from locality to locality
• locality: a set of pages actively used together
Understanding the Locality Model
• Locality of Reference states that a process tends to access a small set of
pages repeatedly for a certain period before moving to another set.
• Two types of locality:
– Temporal Locality: Recently accessed pages are likely to be accessed again
soon.
– Spatial Locality: Nearby memory locations are likely to be accessed together.
56
Locality Model
57
Locality Model
Working Set and Thrashing Prevention
• Thrashing occurs when a system does not allocate enough frames to a
process, causing frequent page faults and excessive swapping.
• The Working Set Model prevents thrashing by ensuring that a process is
allocated enough frames to hold its working set.
• If total available frames < total working set pages of all active processes,
the system reduces the degree of multiprogramming by suspending some
processes.
58
Locality Model
Working Set Algorithm
• Monitor each process's working set to track which pages it needs.
• Allocate enough frames to each process to store its working set.
• If the total working set size exceeds available memory, suspend some
processes.
• Resume suspended processes when enough memory is available.
59
Locality Model
Example
• Suppose P1 needs {A, B, C}, P2 needs {D, E, F, G}, and P3 needs {H, I, J}.
• If total available frames = 6 and total working set pages = 10, the system
will suspend P2 to reduce memory demand.
• When memory is available again, P2 is resumed.
60
Working-Set Model
Advantages
• Reduces page faults by ensuring essential pages remain in memory.
• Prevents thrashing by dynamically adjusting the number of active
processes.
• Improves CPU utilization by keeping only necessary processes active.
Disadvantages
• Overhead in tracking the working set for each process.
• Requires tuning of Δ to balance accuracy and efficiency.
• Process suspension delays execution but helps prevent system slowdown
61
Page Fault Frequency
62
Page Fault Frequency
63
Page Fault Frequency
64
Page Fault Frequency
65
Page Fault Frequency
Example Scenario
• Assume a system has 4 processes (P1, P2, P3, P4) and 20 available
frames.
• Each process initially gets 5 frames.
• The system observes the PFF values after execution:
– P1 → High PFF → Needs more frames.
– P2 → Low PFF → Can release some frames.
– P3 → High PFF → Needs more frames.
– P4 → Low PFF → Can release some frames.
• The system reallocates frames accordingly to optimize performance and
prevent thrashing.
66
Page Fault Frequency
67
Page Fault Frequency
68