Virtual Memory & Memory Mapping

swapping and swap space

When physical RAM fills up but programs still need more, the OS borrows disk: it copies pages that have not been used lately out to a reserved area on disk, frees the frames they occupied for someone else, and brings those pages back if and when they are touched again. Swapping (also called paging to disk) is this moving of pages between RAM and disk; the reserved disk area is the swap space (a swap partition or a swap file).

Step by step: when RAM is under pressure, the OS picks a victim page that is unlikely to be needed soon (using the accessed and dirty bits to guess), writes it to swap space if it has been modified, and marks its page-table entry not-present while remembering where on disk it now sits. The frame is now free. Later, if the program touches that virtual address again, the MMU raises a page fault; the OS reads the page back from swap into a fresh frame, fixes the entry, and resumes. The program sees only a pause — its data appears intact, having silently round-tripped through disk.

This is what lets a machine run a working set larger than its RAM, but the trade-off is brutally physical: disk is many thousands of times slower than RAM, so every swapped page that has to come back is an expensive major page fault. A little swapping smooths over peaks; heavy, continuous swapping is thrashing, where the machine spends more time shuffling pages than computing and grinds to a near halt. Swap extends capacity gracefully only as long as the truly hot data still fits in RAM.

Open too many heavy applications and free RAM drops near zero; the OS starts writing idle pages to swap. Switching back to an app you left alone for a while feels sluggish for a moment — that pause is its pages being read back from swap on demand.

Idle pages park on disk; touching them again pays a slow read back.

Swap is not 'more RAM' — it is far slower disk standing in for RAM under pressure. Adding swap can keep a machine alive but cannot make it fast; if the hot working set exceeds RAM, the result is thrashing, not speed.

Also called
paging to diskswappage file置換置換空間