Virtual memory

On this page 7

Virtual memory is the operating system technique that gives each process its own private, contiguous address space, which the kernel and the processor’s memory management unit translate onto physical RAM and, when needed, onto disk storage. Among OS fundamentals it is one of the core building blocks, because it underpins process isolation, efficient memory sharing and the ability to run programs larger than the free RAM.

A program never sees physical addresses directly. It works with virtual addresses, and the hardware translates every memory access through page tables that the operating system maintains. This indirection lets the system move, share, protect and evict memory without the program noticing.

How virtual memory translates addresses

Virtual memory divides both the virtual address space and physical memory into fixed-size blocks: pages on the virtual side and frames on the physical side. A page table records, for each virtual page, which frame holds it, along with permission bits such as read, write, execute and user or kernel access.

The memory management unit (MMU) performs the translation in hardware on every access. Because walking multi-level page tables is slow, the processor caches recent translations in a translation lookaside buffer (TLB). A TLB hit costs almost nothing, while a miss forces a page table walk of four or five levels on modern 64-bit processors.

If the page table entry says the page is not present, the MMU raises a page fault, an exception handled by the kernel. The kernel then decides whether the access is legal, finds or allocates a frame, loads data if necessary, updates the page table and resumes the process as if nothing happened.

ParameterTypical valueWhat it affects
Base page size4 KiB on x86-64; 4 or 16 KiB on ArmGranularity of mapping and protection
Large pages2 MiB and 1 GiB on x86-64Fewer TLB misses for big data sets
Virtual address width48 or 57 bits on x86-64Size of each process address space
Page table levels4 or 5 on x86-64Cost of a TLB miss
TLB entriesTens to a few thousandHow much memory is covered without walks
Key virtual memory parameters

Paging to disk and demand loading

Virtual memory is often equated with swapping, but disk backing is only one of its uses. The kernel loads pages on demand: executable code is read from the program file only when first touched, and newly allocated memory receives a physical frame only when first written.

When RAM runs short, the kernel selects pages that have not been used recently and evicts them. Clean pages backed by files are simply discarded and reread later, while modified anonymous pages are written to a swap area or page file. This is the paging mechanism that lets the total committed memory exceed physical RAM.

Fault typeCauseTypical cost
Minor (soft) faultPage is in RAM but not yet mappedMicroseconds
Major (hard) faultPage must be read from diskTens of microseconds on SSD, milliseconds on hard disk
Invalid accessAddress not mapped or permission deniedProcess receives an error such as a segmentation fault
Types of page faults

The cost gap is the key to performance. A major fault is thousands of times slower than a RAM access, so a system that pages heavily becomes sluggish even though it keeps working.

Virtual memory sits at the center of several neighboring concepts in operating system design. Separating them avoids a common source of confusion in documentation and settings dialogs.

ConceptWhat it isRelation to virtual memory
Memory managementAll kernel work on allocating and tracking memoryVirtual memory is its central mechanism
PagingDividing memory into pages and moving themThe usual implementation of virtual memory
Swap space or page fileDisk area for evicted anonymous pagesOptional backing store, not the whole concept
SegmentationVariable-size memory regions with bases and limitsOlder scheme, largely replaced by paging
Physical memoryInstalled RAMThe resource that virtual memory maps onto
Virtual memory and neighboring terms

Windows labels the page file settings as virtual memory, which reinforces the idea that the two are identical. In reality, disabling the page file does not disable virtual memory; every process still runs in its own translated address space.

What virtual memory makes possible

Beyond fitting large programs into limited RAM, virtual memory provides several features that modern operating systems depend on.

  • Process isolation: one process cannot read or corrupt another process’s memory, because their page tables map to different frames.
  • Kernel protection: kernel space pages are marked inaccessible from user space.
  • Shared libraries: one physical copy of library code is mapped into many processes.
  • Copy-on-write: a forked process shares pages with its parent until one of them writes.
  • Memory-mapped files: files appear as memory, and the kernel loads and writes pages automatically.
  • Execute protection: data pages marked non-executable block a large class of code injection attacks.

Common problems and tuning mistakes

The most serious virtual memory problem is thrashing: the combined working sets of running processes exceed RAM, so the system spends most of its time moving pages to and from disk. The symptom is a disk that stays busy while the processor waits, and the cure is more RAM or fewer simultaneous workloads, not a bigger page file.

  • Disabling swap entirely can cause programs to be terminated by out-of-memory handling instead of slowing down gracefully.
  • Oversized page files waste disk space without improving speed once RAM is sufficient.
  • Ignoring TLB pressure in databases and virtual machines leaves performance unused that large pages could recover.
  • Reading committed memory as used RAM leads to wrong conclusions, because committed memory is a promise, not occupied frames.

In everyday language

Virtual memory means every program believes it has a large, private block of memory all to itself. In reality, the computer keeps a translation list that says where each piece really sits, in RAM or temporarily on the disk.

It is similar to a library catalog. Readers ask for a book by its catalog number and never need to know which shelf or storage room holds it. The librarian can move books around freely. The program asks for an address, and the system finds where the data really is.

Seen in the real world

A video editor works on a laptop with 16 GB of RAM while a browser with dozens of tabs stays open in the background. When the editor loads a large project, the operating system quietly evicts browser pages that have not been touched for a while, writing them to the page file on the SSD.

The editing software keeps running smoothly because its active data stays in RAM. When the user later switches back to an old browser tab, there is a short pause of a fraction of a second while those pages are read back, which is a burst of major page faults handled by the kernel.

When the editor adds a second large project, the combined working set exceeds RAM, the SSD stays busy and the whole system stutters. Closing the browser ends the thrashing at once, showing that virtual memory stretches RAM but cannot replace it.