virtual memory, MMUs, CPUs, RAM, page tables, page faults, forking


What is Virtual Memory?

At its core, virtual memory is a clever trick that allows a computer to act as though it has more memory than it really does. It creates an illusion for programs, letting them think they have a large, private memory space, even though the actual physical memory (RAM) is limited and shared among all programs.


Imagine This Scenario

Think of your computer's memory as a library:

  1. Physical memory (RAM): This is like the bookshelf space in the library—it's limited. If the shelves are full, you can't immediately add more books.
  2. Storage (Hard Drive/SSD): This is like a storage room in the library basement—it's much larger than the shelves, but it's slower to access. If there's no room on the shelves, you move books to or from the basement.
  3. Virtual memory: This is a clever librarian who hands each visitor their own "pretend" bookshelf (virtual address space). The visitor can work with this bookshelf as if it's fully theirs, even if behind the scenes, the librarian is juggling books between the real shelves and the basement.

How Virtual Memory Works

  1. Programs Think They Have Their Own Memory:

    Each program gets its own virtual memory space. For example, if two programs are running, they both think they're using the same memory addresses, but these addresses are mapped to different physical locations.

  2. Memory Management Unit (MMU):

    The MMU is hardware that translates virtual addresses (the pretend bookshelf) into physical addresses (the real bookshelf). It uses the page table, a structure maintained by the operating system (OS), to find where the data is located.

  3. Page Table:

    The page table maps virtual addresses to physical addresses. Each program has its own page table. It includes metadata about each page, such as:

  4. Swapping (or Paging):

    If there's not enough physical memory to hold everything, the OS temporarily moves some data to the hard drive (like putting books in the basement). When the program needs those books again, the OS swaps them back to the shelves. This is slower than using RAM but ensures programs don't crash due to memory shortages.

  5. Demand Paging:

    Only the portions of a program actively being used are loaded into RAM. This saves space by delaying the loading of unused sections.


Page Faults

A page fault occurs when a program accesses data not currently in RAM. Here's the process: