OSOperating Systems · Lesson 4 of 7

Memory: Virtual, Paged, Shared

Every process believes it has the entire address space to itself. It's lying — or rather, the OS is lying to it, beautifully. Virtual memory is the OS's single best idea.

Addresses your program uses are virtual; hardware translates them to physical RAM through per-process page tables, in 4KB chunks called pages. Consequences: processes can't see each other's memory (isolation), each can lay out its memory the same way (simplicity), and the same physical page can appear in many processes (shared libraries — one copy of libc for a thousand processes).

Text
Virtual memory tricks, all from one mechanism:

  Lazy allocation   malloc(1GB) instantly 'succeeds' — pages
                    materialize only when actually touched
  Swap              rarely-used pages evicted to disk; RAM
                    'overcommitted' beyond physical size
  Memory-mapped IO  a file mapped into your address space —
                    reading memory reads the file (mmap)
  Copy-on-write     fork() copies nothing; pages duplicate
                    only when parent or child writes one

A 'page fault' = CPU touched a page with no mapping.
Minor fault: kernel fixes it up (lazy alloc). Normal.
Major fault: page must come from disk. Slow.
Segfault: no legal mapping ever — program dies.
Bash
free -h                  # total / used / available RAM
# 'available' counts cache the kernel will give back —
# 'Linux ate my RAM' is free RAM being used as disk cache.

cat /proc/$$/status | grep -E 'VmSize|VmRSS'
# VmSize: virtual size (promises). VmRSS: real RAM (truth).

# Watch a memory hog get killed:
# When RAM + swap run out, the kernel's OOM killer
# picks the biggest offender and SIGKILLs it.
dmesg | grep -i "out of memory"
◆ Note
This is the machinery under every language's memory story: C's malloc asks the kernel for pages and carves them up; Python/Java/JS garbage collectors manage objects inside pages the same way. When a process's RSS climbs and never falls, that's a leak — in any language.