Lesson 4 of 7
Memory: Virtual, Paged, Shared
Each sees its own private address space, even though they all share the same physical RAM. You'll learn how virtual makes this work and what it enables.
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).
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.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"Finished reading?Mark it done to track your progress.
Tip: use the ← and → keys to move between lessons.