Virtual memory and paging
In short: Every process sees its own contiguous address space. The operating system maps these virtual addresses to real memory via page tables and moves pages to disk when needed.
In more detail: Memory is divided into pages (usually 4 KB). The CPU’s MMU translates virtual into physical addresses on every access. A TLB (cache for translations) makes this fast. If a page is not in RAM the CPU raises a page fault and the operating system loads it.
In Depth
- Isolation: Processes cannot read other processes’ memory.
- Page tables: Multi-level (for example 4 levels on x86-64) to keep them small.
- Swapping: Rarely used pages move to disk. Too much of it is thrashing, the system crawls.
- Copy-on-write:
fork()shares pages and copies only on write. - Memory-mapped files: Map files like memory.
- Overcommit: Promise more than physically exists (Linux), OOM killer as emergency brake.
- Huge pages: Fewer TLB misses with large data.
See also: Process scheduling, Memory hierarchy and cache lines