Interview Prep Zoneby CuriouserLabs

Question 3 of 5 · staff level

Minor vs major page faults, demand paging and copy-on-write — what happens on each, and what do fault rates tell you in production?

🎤 Say this first

A page fault is the MMU saying 'no valid translation' — and it's usually normal operation, not an error. Minor fault: the page needs no disk I/O — first touch of an allocated page (zero-fill), a COW copy, or the page was already in page cache — microseconds. Major fault: the content must come from disk (swapped-out page, first read of an mmapped file) — milliseconds, five orders of magnitude worse. Demand paging means everything is lazy: exec maps the binary, pages arrive as executed. In production: sustained major faults = memory pressure or cold mmap access — a leading indicator that latency is about to die.

The full picture

  • Fault taxonomy: zero-fill (first malloc touch), COW break (post-fork write), page-cache hit mapping (minor), swap-in / file read-in (major), and the invalid access that actually is a bug → SIGSEGV. Same trap, five meanings — the kernel disambiguates via the VMA (memory-region metadata).
  • Why 'allocated' isn't 'paid for': calloc of 1GB is instant; the cost arrives as ~262k minor faults as you touch it. Pre-touching (-XX:+AlwaysPreTouch) moves that cost to startup — why low-latency JVMs pay it there instead of during requests.
  • Reading the metrics: ps/pidstat min_flt/maj_flt, vmstat si/so. Minor fault storms = churny allocation or COW after fork (Redis snapshot!); any sustained majfl on a latency-critical service = swapping or cold file access — act now.
  • Swap policy for services: the modern stance is not 'swap off' dogma but 'swap ≠ latency path' — many operators run swap off or tiny for JVM fleets because a GC walking swapped heap pages is catastrophic; K8s historically required swap off for accounting sanity.

🔄 Likely follow-up questions

  • Why does AlwaysPreTouch exist and what workloads justify it?
  • Walk through exactly what happens when Redis forks for BGSAVE under write load.
  • How does mlock() interact with this, and who needs it (trading, crypto keys)?