← All posts
7 min read

Virtual Memory: Private Address Spaces Built on Physical RAM

#technology#computer-science#virtual-memory#operating-systems
📑 On this page

Two applications can both use an address such as 0x1000 without accessing the same physical byte. A program can also reserve a large address range before all of it exists in RAM.

Virtual memory makes this possible.

Virtual memory gives each process its own address space and uses mappings to connect virtual pages with physical RAM, files, or temporarily absent data.

It is primarily an address-management and protection system. Using storage as overflow is only one feature.

Virtual and physical addresses

Program instructions use virtual addresses.

The processor's memory-management unit translates them into physical addresses according to tables maintained by the operating system.

Process A's virtual page can map to one physical frame, while process B's identical-looking virtual page maps elsewhere.

Applications gain a simple private view even when physical memory is fragmented and shared dynamically.

Pages and frames

Memory is divided into fixed-size blocks.

  • A page is a block in virtual address space.
  • A frame is a corresponding block in physical RAM.

Common page sizes include 4 KiB, with larger pages available on many systems.

Page tables record mappings and permissions. A page may be:

  • Present in RAM
  • Read-only
  • Executable or non-executable
  • Shared
  • Backed by a file
  • Not currently present

Managing blocks instead of every byte individually keeps translation practical.

Page tables and the TLB

Walking page-table structures for every memory access would be slow.

Processors use a translation lookaside buffer, or TLB, to cache recent virtual-to-physical translations.

A TLB hit provides a quick mapping. A TLB miss requires a page-table walk, often performed partly by hardware.

Large applications with scattered access patterns can pressure both data caches and translation caches.

This is one reason page size and memory locality affect performance.

Isolation and permissions

Page-table entries include access permissions.

The OS can prevent:

  • User code from reading kernel memory
  • One process from accessing another process
  • Writing to read-only code
  • Executing ordinary data pages

If a process violates a mapping, the processor raises a fault and enters the kernel. The OS may terminate the process with an access-violation or segmentation-fault error.

This hardware-enforced boundary is fundamental to operating-system security.

Demand paging

The OS does not need to load an entire application into RAM immediately.

With demand paging, pages are loaded when first accessed.

Launching a large program can therefore map its executable files into virtual memory without reading every section. Unused features may never consume RAM.

The first access to an absent but valid page causes a page fault:

  1. The processor detects no present mapping.
  2. The kernel identifies the required data.
  3. It obtains a free frame.
  4. It reads or creates the page.
  5. It updates the mapping.
  6. The instruction resumes.

A page fault can be a normal mechanism, not necessarily an error.

Swapping and memory pressure

When physical RAM becomes scarce, the OS may move less-active anonymous pages to a swap file or partition.

Later access causes a fault and reads the page back.

Storage is much slower than RAM. Occasional swapping can preserve functionality; heavy swapping creates long pauses.

Virtual memory does not make storage equivalent to extra RAM. It lets the OS trade performance for capacity under pressure.

Compressed-memory systems may compress inactive pages in RAM before writing them to storage, trading CPU work for reduced I/O.

A concrete example: several applications

Suppose three applications each reserve 4 GB of virtual address space.

That does not necessarily require 12 GB of physical RAM immediately.

  • Some regions may be unused.
  • Executable pages can be loaded on demand.
  • Shared libraries can map the same physical pages.
  • Inactive pages can be reclaimed or swapped.
  • Each process sees its own addresses.

If all applications actively touch large private data sets, physical demand becomes real and the system may experience memory pressure.

Reservation, mapping, and active resident memory are different measurements.

File-backed memory

Files can be mapped directly into virtual address space.

The application accesses mapped bytes as memory. The OS loads pages on demand and may write modified pages back.

Benefits include:

  • Convenient random access
  • Shared caching with normal file I/O
  • Sharing among processes
  • Avoiding some explicit copies

Memory-mapped files still require careful handling of file size, synchronization, errors, and durability.

Executable programs and shared libraries commonly use file-backed mappings.

Copy-on-write

Two processes can temporarily share the same physical page as read-only.

If one attempts to modify it, the kernel creates a private copy and updates that process's mapping. This is copy-on-write.

The technique saves memory and startup work when processes begin with mostly identical data.

It is used in process creation, virtual machines, containers, file systems, and snapshots.

Sharing continues until a write makes separation necessary.

Address-space layout

A process address space commonly contains regions for:

  • Program code
  • Static data
  • Heap allocations
  • Thread stacks
  • Shared libraries
  • Mapped files

The exact arrangement varies and can be randomized for security through address-space layout randomization.

On 64-bit systems, the theoretical address range is enormous, though processors and operating systems implement fewer bits and impose limits.

A large virtual range does not imply equal physical RAM or storage.

Memory leaks and allocation failures

A memory leak occurs when software retains allocations it no longer needs.

Virtual and physical usage can grow until:

  • The process slows
  • The OS swaps heavily
  • Allocation fails
  • The process is terminated

Garbage-collected languages reduce some manual deallocation errors, but references can still keep unused objects alive.

Monitoring tools distinguish private memory, shared memory, committed space, and resident physical pages. One "memory usage" number may not explain the whole situation.

Common misunderstandings

"Virtual memory means swap space"

Swap is one feature. Virtual memory fundamentally provides address translation, isolation, permissions, and flexible mapping.

"A 64-bit process has an unlimited amount of RAM"

It has a large address range, but physical memory, storage, OS policy, and application limits remain finite.

"Every page fault indicates a crash"

Demand paging routinely uses recoverable page faults. Invalid accesses cause failures.

"Allocated memory always occupies RAM immediately"

Some allocation reserves address space, and physical pages are committed or loaded only when touched.

Knowledge check

1. Why can two processes use the same virtual address safely?

Their page tables can map that virtual address to different physical frames.

2. What is a page fault?

It is a processor event indicating that a memory access needs kernel handling, either to load a valid page or reject an invalid access.

3. Why is heavy swapping slow?

Pages must move between RAM and much slower storage, making threads wait repeatedly.

4. What does copy-on-write accomplish?

It lets processes share a physical page until one writes, delaying copying and saving memory.

The one idea to remember

Virtual memory is a mapping system that gives each process a protected, flexible address space over finite physical resources.

It enables isolation, demand loading, file mapping, sharing, and controlled fallback to storage.

Next, we will build on storage abstractions by examining files, folders, and paths.