Virtual Memory in Operating System

Subject: Computer Science Fundamentals · Language: Operating System · Level: intermediate · 10 min read

Understand virtual memory, demand paging, page faults, replacement algorithms, thrashing, and why programs can run beyond physical RAM.

Virtual memory is a memory management technique that allows a computer to run programs even when the complete program cannot fit inside physical RAM at once. It gives each process the feeling that it has a large, continuous memory space, even though the actual RAM may be limited. A beginner can think of virtual memory like studying with a small desk and a large bookshelf. You cannot keep every book open on the desk at the same time, so you keep only the books or pages you are currently using on the desk. The remaining books stay on the shelf and are brought to the desk only when needed. In the same way, the OS keeps required parts of a program in RAM and keeps other parts on disk until they are needed.

Virtual memory is important because modern programs can be large, and many programs may run at the same time. Without virtual memory, a process would need all of its required memory to be available in RAM before it could run. That would waste memory and reduce multitasking.

Why Virtual Memory Is Needed

RAM is faster than secondary storage, but RAM is limited. A computer may have many processes running together, and each process may need memory for code, data, stack, heap, libraries, and temporary work. If the operating system forced every process to load fully into RAM, only a few programs could run at a time. Virtual memory solves this by loading only the required parts of a process into RAM. Virtual memory also helps isolate processes from each other. Each process gets its own virtual address space, so one process cannot directly access another process's memory unless the operating system allows it.

Virtual Address Space

The virtual address space is the memory view given to a process. From the process's point of view, it appears to have a large continuous block of memory starting from low addresses and going up to high addresses. This does not mean the whole block is physically present in RAM. The operating system and hardware translate virtual addresses into physical addresses whenever the process accesses memory. This separation is useful because the program does not need to know where its data is actually stored in RAM. The OS handles the mapping in the background.

Physical Memory

Physical memory means the actual RAM installed in the computer. It is limited and shared among the operating system, user programs, device buffers, and other running processes. Virtual memory makes physical memory appear larger and more flexible. However, it does not make RAM magically infinite. If too many pages are constantly needed, performance can become slow because the OS has to move data between RAM and disk frequently.

Pages and Frames

Virtual memory commonly uses paging. In paging, the virtual address space of a process is divided into fixed-size blocks called pages. Physical memory is divided into fixed-size blocks called frames. A page from virtual memory can be loaded into any free frame in physical memory. For example, if the page size is 4 KB, then the process memory is divided into 4 KB pages, and RAM is divided into 4 KB frames. The OS keeps track of which page is stored in which frame.

Page Table

A page table is a data structure used by the operating system to map virtual pages to physical frames. Each process usually has its own page table. When a process uses a virtual address, the CPU and memory management unit check the page table to find the corresponding physical frame. After the frame is found, the actual physical address can be used to access RAM. A page table entry may contain the frame number, a valid bit, protection information, dirty bit, reference bit, and other control information.

Valid Bit

The valid bit tells whether a page is currently present in physical memory. If the valid bit is set, the page is in RAM and can be accessed normally. If the valid bit is not set, the page is not currently in RAM. It may be on disk, or it may not belong to the process at all. If the process tries to access such a page, a page fault may occur.

Demand Paging

Demand paging means pages are loaded into RAM only when they are actually needed. The OS does not load the entire process at the beginning. This saves memory because unused parts of a program do not occupy RAM. For example, a large application may contain many features, but the user may use only a small part of it during one session. Demand paging improves memory usage and startup time, but it depends on the OS handling page faults efficiently.

Page Fault

A page fault occurs when a process tries to access a page that is not currently present in RAM. This does not always mean the program has an error. In virtual memory, page faults are a normal part of demand paging. When a page fault occurs, the operating system pauses the process, finds the required page on disk, loads it into a free frame in RAM, updates the page table, and then resumes the process. If there is no free frame available, the OS must choose some existing page in RAM to remove. This decision is handled by a page replacement algorithm.

Steps in Handling a Page Fault

First, the process refers to a page that is not in RAM. The hardware detects this using the page table and transfers control to the operating system. Next, the OS checks whether the memory reference is valid. If the reference is invalid, the process may be terminated because it tried to access memory it should not access.

If the reference is valid, the OS locates the page on disk. It then finds a free frame or selects a victim page to remove from RAM. After the required page is loaded into RAM, the page table is updated. Finally, the instruction that caused the page fault is restarted, and the process continues execution.

Backing Store

The backing store is the area on disk used to keep pages that are not currently in RAM. It may be a swap partition, swap file, or another disk area managed by the operating system. Disk is much slower than RAM, so accessing pages from the backing store takes more time. This is why too many page faults can make a system slow.

Page Replacement

Page replacement is needed when RAM is full and a new page must be loaded. The OS chooses one page already in memory and removes it to make space for the required page. The removed page is called the victim page. If the victim page was modified, it must be written back to disk before removal. If it was not modified, it can simply be discarded because the original copy already exists on disk. The goal of page replacement is to remove a page that is least likely to be needed soon. A good replacement decision reduces future page faults.

FIFO Page Replacement

FIFO stands for First In, First Out. In this algorithm, the page that entered memory first is removed first. FIFO is easy to understand and implement, but it may remove an important page just because it is old. A page can be old and still be frequently used, so FIFO is not always efficient.

Optimal Page Replacement

Optimal page replacement removes the page that will not be used for the longest time in the future. This gives the best possible result in theory. The problem is that the OS cannot know the future page references of a running process. Because of this, optimal replacement is mainly used for comparison and study, not practical implementation.

LRU Page Replacement

LRU stands for Least Recently Used. It removes the page that has not been used for the longest time in the recent past. The idea is that recent usage often gives a hint about future usage. If a page has not been used for a long time, it may be less likely to be needed soon. LRU is more realistic than optimal replacement, but implementing exact LRU can be costly because the OS must track page usage history.

Thrashing

Thrashing is a condition where the system spends more time swapping pages between RAM and disk than executing actual process instructions. This happens when too many processes are competing for limited memory, or when a process does not have enough frames to hold its active pages. When thrashing occurs, CPU utilization may become low even though the system feels busy. The disk keeps working heavily, but useful program progress becomes slow.

Working Set

The working set of a process is the group of pages that the process is actively using during a period of time. If the working set is kept in RAM, the process can run with fewer page faults. If the OS gives a process fewer frames than its working set needs, the process may keep generating page faults. This can lead to thrashing. The working set idea helps the OS understand how much memory a process really needs at a particular time.

Virtual Memory and Multitasking

Virtual memory supports multitasking because it allows several processes to share RAM efficiently. Each process keeps only its needed pages in memory. The OS can move less active pages to disk and keep active pages in RAM. This helps the computer run multiple programs even when total process memory is larger than physical RAM.

Advantages of Virtual Memory

Virtual memory allows large programs to run even when physical RAM is limited. It improves memory utilization because only required pages are loaded. It also supports process isolation because each process works in its own virtual address space. This improves security and stability. Another advantage is easier programming. Programmers do not need to manually manage physical memory locations because the OS and hardware handle address translation.

Limitations of Virtual Memory

Virtual memory can slow down the system if page faults are frequent. RAM access is fast, but disk access is much slower. It also requires extra data structures such as page tables. Large page tables can consume memory, so modern systems use techniques like multilevel page tables and translation lookaside buffers to improve efficiency. Virtual memory is powerful, but it must be managed carefully. Too many active programs with limited RAM can still make the system slow.

TLB in Virtual Memory

TLB stands for Translation Lookaside Buffer. It is a small, fast cache that stores recent virtual-to-physical address translations. Without a TLB, every memory access may require checking the page table, which can slow down execution. The TLB makes address translation faster by remembering recently used mappings. If the required mapping is found in the TLB, it is called a TLB hit. If it is not found, it is called a TLB miss, and the page table must be checked.

Summary

Virtual memory gives each process a large and protected memory view while using physical RAM efficiently. It works mainly through paging, page tables, demand paging, and page fault handling. The main benefit is better multitasking and the ability to run large programs. The main risk is performance loss when page faults become too frequent. For beginners, the key idea is simple: only the currently needed parts of a program stay in RAM, and the remaining parts are brought from disk when required.

More Operating System Lessons

Browse all PrepCampus study materials