CodePark/ questions / Memory, stack & heap
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › Memory, stack & heap

What is virtual memory, and what does it give the operating system?

With virtual memory, every process sees its own private address space. The CPU has a memory management unit (MMU). It translates virtual addresses to physical memory using page tables, one set per process. Memory is split into fixed-size pages, usually 4 KB on x86-64.

What this gives the operating system:

  • Isolation. A process can only reach pages mapped in its own tables. So it can't read another process's memory.
  • Lazy allocation. A page gets physical RAM only when the process first touches it. That first touch triggers a page fault, and the kernel then gives it RAM.
  • Sharing. Processes can share pages, like the code of a shared library.
  • Copy-on-write. After fork(), parent and child share pages. A page is copied only when one of them writes to it.
  • Swap. When RAM runs low, the kernel can drop page cache or move anonymous pages (memory not backed by a file, like the heap) to swap on disk. A later page fault brings them back.

This is why a process's virtual size (VSZ) can be far larger than its resident size (RSS, the part actually in RAM). It is also why heavy swapping makes a machine very slow.

  • Each process gets its own private virtual address space.
  • Page tables map virtual pages to physical frames, and the MMU uses them to translate addresses.
  • Virtual memory isolates processes from each other.
  • It allows lazy allocation through page faults, page sharing and copy-on-write.
  • Swap lets pages move to disk, so VSZ can be larger than RSS.