Computer Basics - Virtual Memory and Paging
\text{Virtual Memory Size} = \text{RAM} + \text{Disk-backed Swap/Page File}
Virtual memory is a memory management technique used by an operating system to make a computer appear as if it has more main memory (RAM) than is physically installed. It does this by using a portion of storage (usually the hard drive or SSD) as an extension of RAM. This extra space is commonly called swap space or the page file.
Virtual memory allows programs to run even when the total memory they need is larger than the available physical RAM. It also helps the operating system manage many running applications at once by moving less-used data temporarily from RAM to storage and bringing it back when needed.
Why virtual memory is needed
Microsoft Windows, Linux, and macOS all rely on virtual memory because modern applications often require more memory than a computer’s physical RAM alone can provide.
For example, if a computer has 4 GB of RAM and the user opens a web browser, document editor, media player, and several background applications, the total memory demand may exceed 4 GB. Instead of closing applications immediately, the operating system can move inactive data to disk-based virtual memory.
This provides:
-
Better multitasking
-
Ability to run large applications
-
Isolation between processes
-
Efficient memory allocation
-
Protection from unauthorized memory access
What virtual memory means
Every program thinks it has access to a large continuous block of memory. In reality, the operating system maps this “virtual” address space to actual physical memory locations.
This means:
-
Programs use virtual addresses
-
The operating system translates them to physical addresses
-
This translation is handled by the Memory Management Unit (MMU) in the processor
The program never directly knows where its data is physically stored.
How virtual memory works
When a program starts:
-
The operating system creates a virtual address space for it
-
The required portions are loaded into RAM
-
Extra parts remain on disk until needed
-
When RAM fills up, inactive pages are moved to disk
-
Needed pages are brought back to RAM when accessed
This process is automatic.
Paging
Paging is the method used to implement virtual memory.
The operating system divides memory into fixed-size blocks:
-
Pages → blocks of virtual memory
-
Frames → blocks of physical RAM
A page is mapped to a frame.
For example:
-
Page size = 4 KB
-
RAM frame size = 4 KB
-
Virtual pages are loaded into available physical frames
The system keeps track of this mapping through a page table.
Page table
The page table is a data structure that records where each virtual page is located.
It stores:
-
Page number
-
Frame number
-
Access permissions
-
Status information
-
Presence in RAM or disk
When a program accesses memory:
-
CPU generates virtual address
-
MMU checks page table
-
Finds physical frame
-
Accesses actual RAM
This makes memory access transparent.
Page fault
A page fault happens when the requested page is not currently in RAM.
The sequence is:
-
Program requests data
-
Page is missing in RAM
-
CPU triggers page fault
-
Operating system loads page from disk
-
Replaces another page if needed
-
Execution continues
Page faults are normal but too many can slow performance.
Types of paging
Demand paging
Pages are loaded only when required.
Advantages:
-
Saves RAM
-
Loads only necessary data
-
Faster initial program loading
Pre-paging
Pages are loaded in advance.
Advantages:
-
Reduces repeated faults
-
Improves performance for predictable access
Swapping
Swapping is related to virtual memory.
It means moving an entire process or large memory section between RAM and disk.
Paging moves smaller units (pages), while swapping may move larger segments.
Address translation
Virtual memory relies on address translation.
Suppose a process accesses address:
4096
This is virtual.
MMU converts it to:
Actual physical RAM address.
Translation occurs using:
-
Page number
-
Offset inside page
Formula:
Physical Address = Frame Number × Page Size + Offset
Benefits of virtual memory
Efficient use of RAM
Only active pages remain in RAM.
Large application support
Programs can use more memory than installed RAM.
Process protection
Each process gets isolated address space.
Multitasking
Many applications run together.
Memory sharing
Libraries can be shared between processes.
Disadvantages
Slower than RAM
Disk is slower than physical memory.
Page faults reduce speed
Frequent page loading delays execution.
Thrashing
Thrashing happens when the system spends too much time moving pages between RAM and disk.
This occurs when:
-
RAM is insufficient
-
Too many programs run simultaneously
Performance becomes very poor.
Real-life example
Suppose a system has:
-
8 GB RAM
-
16 GB virtual memory
User opens:
-
Browser
-
Spreadsheet
-
Video editor
-
IDE
-
Music player
Inactive browser tabs may move to disk.
When reopened:
-
Pages return to RAM
-
User continues normally
This makes the system feel capable of handling larger workloads.
Virtual memory in operating systems
Microsoft Windows
Uses page file:
-
pagefile.sys
Stored on disk.
Linux
Uses swap partition or swap file.
macOS
Uses dynamic swap management.
Importance
Virtual memory is fundamental because it separates application memory from physical hardware limitations.
Without virtual memory:
-
Large programs may fail
-
Multitasking would be limited
-
System stability would decrease
-
Memory protection would be weaker
Summary
Virtual memory is a system that extends RAM using disk storage. It gives programs an illusion of larger memory space. Paging divides memory into fixed blocks and moves them between RAM and storage as required.
This mechanism improves:
-
Performance
-
Flexibility
-
Security
-
Multitasking
-
Resource management
Virtual memory is one of the most important concepts in computer architecture because it enables modern operating systems to run multiple applications efficiently even with limited physical RAM.