What is swapping?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Operating Systems basics — a prerequisite for any developer role.

Answer

Swapping is the technique of temporarily moving a process or portions of a process from main memory (RAM) to secondary storage (disk/SSD — swap space) to free up RAM for other processes, then bringing it back when needed. Process-level swapping (traditional): entire processes are moved to and from swap. When memory is scarce, the OS selects a blocked or low-priority process and "swaps it out" — copies its entire address space to swap space, freeing its RAM frames. When the process is needed again, it's "swapped in" back to RAM. Page-level swapping (modern — demand paging): rather than swapping entire processes, individual pages are evicted to swap when frames are needed. This is what modern OSes do. The OS swaps out pages (not whole processes) using page replacement algorithms. Swap space: dedicated disk partition or file used for temporary storage of evicted pages/processes. Linux: /proc/swaps; Windows: pagefile.sys; macOS: compresses memory first, uses swap as last resort. Performance impact: disk I/O is orders of magnitude slower than RAM (ms vs ns). Heavy swapping = thrashing → severe performance degradation. Symptom: high disk activity, CPU mostly waiting, poor responsiveness. Modern trends: SSDs made swap less painful but still slow. Mobile OSes (iOS) don't use traditional swap — they kill background processes. Linux now uses zswap (compress pages before swapping) and zram (RAM as compressed swap). Best practice: have enough RAM to avoid swapping for production systems.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Operating Systems experience.