Labs ICT
โญ Pro Login

Virtual Memory

The illusion that every process has its own private memory.

The Illusion of Infinite Memory

Here's a mind-bending idea: what if every process could act as if it has access to more memory than actually exists? What if your laptop with 8GB of RAM could run processes that each think they have 4GB to themselves โ€” even if you have 20 processes running?

That's exactly what virtual memory does. It separates the memory that a process uses from the memory that actually exists in hardware. Each process gets its own virtual address space, and the OS maps virtual addresses to physical addresses behind the scenes.

How Virtual Memory Works

Virtual memory uses paging to create the illusion. Each process has its own page table that maps virtual pages to physical frames. But here's the trick: not all pages need to be in physical memory at the same time.

When a process accesses a page that's currently in RAM, the translation happens normally. But when it accesses a page that's on disk (not in RAM), a page fault occurs. The OS interrupts the process, loads the required page from disk into RAM, updates the page table, and resumes the process โ€” all transparently.

The process never knows the difference. It thinks all its pages are in memory all the time.


  Virtual Memory Diagram:

  Process A          Process B          Physical RAM         Disk (Swap)
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚ Page 0   โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ†’ Frame 3 โ”‚      โ”‚ Frame 0: P-A0โ”‚     โ”‚ Page 1-A โ”‚
  โ”‚ Page 1   โ”‚โ”€โ”    โ”‚          โ”‚      โ”‚ Frame 1: P-B2โ”‚     โ”‚ Page 2-A โ”‚
  โ”‚ Page 2   โ”‚ โ”‚    โ”‚ Page 0   โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ†’ Frame 5     โ”‚     โ”‚ Page 3-B โ”‚
  โ”‚ Page 3   โ”‚ โ”‚    โ”‚ Page 1   โ”‚      โ”‚ Frame 2: P-B0โ”‚     โ”‚          โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚    โ”‚ Page 2   โ”‚โ”€โ”€โ”   โ”‚ Frame 3: P-A0โ”‚     โ”‚          โ”‚
               โ”‚    โ”‚ Page 3   โ”‚  โ”‚   โ”‚ Frame 4: P-B1โ”‚     โ”‚          โ”‚
               โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚   โ”‚ Frame 5: P-A2โ”‚     โ”‚          โ”‚
               โ”‚                  โ””โ”€โ”€โ†’โ”‚ Frame 6: P-A3โ”‚     โ”‚          โ”‚
               โ”‚                      โ”‚ Frame 7: P-B3โ”‚     โ”‚          โ”‚
               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’โ”‚ (empty)      โ”‚     โ”‚          โ”‚
                                      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

  Each process has its own virtual        Pages on disk cause PAGE FAULTS
  address space (virtual pages).          when accessed โ€” OS loads them into
  Page tables map to physical frames.     available frames first.

Benefits of Virtual Memory

  • Larger address space โ€” Processes can use more memory than physically available. A 32-bit process can address 4GB of memory even if the system only has 1GB of RAM.
  • Process isolation โ€” Each process has its own virtual address space. One process can't access another's memory because their page tables map to different physical frames.
  • Efficient memory use โ€” Only the pages that are actually being used need to be in RAM. The rest can live on disk. This allows more processes to coexist.
  • Shared memory โ€” Multiple processes can share the same physical frames through different virtual addresses. This is how shared libraries work โ€” one copy of the library code exists in RAM, but multiple processes map it into their virtual address spaces.
  • Memory-mapped files โ€” Files can be mapped into virtual memory, allowing programs to read and write files as if they were regular memory arrays.

The Thrashing Problem

Virtual memory has a dark side. If the system runs too many processes or processes use too much memory, it can enter a state called thrashing. This happens when the OS is constantly swapping pages in and out of memory โ€” spending more time handling page faults than doing actual work.

During thrashing, the system becomes extremely slow. The disk is working overtime, the CPU is mostly idle (waiting for pages), and the user experience is terrible.

The OS monitors page fault rates and takes action when thrashing is detected โ€” typically by swapping out entire processes or killing low-priority ones to free up memory.

๐Ÿงช Quick Quiz

What is thrashing?