What is Swapping in Operating Systems?
Learn what swapping is — moving pages between RAM and swap space, LRU eviction, and thrashing risk — OS interview question answered.
Expected Interview Answer
Swapping is the OS technique of moving an entire process or individual memory pages out of physical RAM to a reserved area of disk (the swap space) to free up RAM, and back in when that memory is needed again.
Classical whole-process swapping moves an entire idle process’s memory image out to disk and later back into RAM, letting more processes exist than physical memory could hold simultaneously, though it is coarse and slow since the whole process image must move at once. Modern operating systems mostly use paging-based swapping instead, where individual pages are evicted to swap space on demand under memory pressure — guided by a page replacement policy such as Least Recently Used — rather than swapping an entire process. When a swapped-out page is later accessed, it causes a page fault, and the kernel reads that specific page back from swap space into a free RAM frame. Swapping is essential for supporting more virtual memory than physical RAM, but it is far slower than RAM access, so heavy, sustained swapping activity degrades into thrashing if the working sets of running processes cannot fit in memory.
- Lets total memory demand exceed installed physical RAM
- Frees RAM for active processes by evicting idle or cold pages
- Page-level swapping is more granular and efficient than whole-process swapping
- Understanding it explains the RAM-vs-disk speed gap and thrashing risk
AI Mentor Explanation
Swapping is like a squad with more contracted players than can fit on the matchday roster, so the team management sends inactive players to a reserve training camp (disk) to free up roster slots for active players, calling them back up when needed. Whole-squad swapping would be like benching an entire team at once, which is slow and disruptive, while modern rosters swap individual players (pages) one at a time as form dictates. Recalling a player from the reserve camp takes time to get them match-fit again, mirroring the slow page fault cost of reading a swapped page back from disk.
Step-by-Step Explanation
Step 1
Memory pressure detected
The kernel observes that free physical RAM is running low relative to demand.
Step 2
Victim selection
A page replacement policy such as LRU selects a cold or idle page (or, classically, an idle process) to evict.
Step 3
Swap-out
The selected page is written to reserved swap space on disk and its physical frame is freed.
Step 4
Swap-in on demand
A later access to the swapped-out page triggers a page fault, and the kernel reads it back from swap space into a free frame.
What Interviewer Expects
- A clear definition of moving memory between RAM and disk swap space
- Distinction between classical whole-process swapping and modern page-level swapping
- Awareness of page replacement policies like LRU choosing what to evict
- Understanding the RAM-vs-disk speed gap and the risk of thrashing under sustained pressure
Common Mistakes
- Confusing swap space with virtual memory as a whole (swapping is one mechanism of it)
- Thinking modern OSes always swap the entire process instead of individual pages
- Not knowing what a page replacement policy is or naming one example
- Ignoring that excessive swapping activity leads to thrashing
Best Answer (HR Friendly)
“Swapping is how an operating system frees up RAM when it is running low, by moving memory that is not currently being used out to a reserved space on disk, and bringing it back in the moment it is needed again. It lets a machine handle more total memory demand than it physically has installed, but because disk is much slower than RAM, doing it too much slows everything down.”
Code Example
struct frame {
int page_id;
long last_used_tick;
};
int select_victim(struct frame *frames, int n, long now) {
int victim = 0;
for (int i = 1; i < n; i++) {
if (frames[i].last_used_tick < frames[victim].last_used_tick)
victim = i; /* pick the least recently used frame */
}
return victim; /* this frame’s page will be written to swap space */
}Follow-up Questions
- What is the difference between whole-process swapping and page-level swapping?
- Which page replacement algorithms decide what gets swapped out?
- How does swapping relate to thrashing?
- Why is SSD-backed swap space still much slower than RAM?
MCQ Practice
1. What is swap space used for?
Swap space is a reserved disk area the OS uses to hold memory contents evicted from RAM so it can be freed for other use.
2. What triggers a swap-in of a page?
Accessing a page that was swapped out raises a page fault, prompting the kernel to read it back from swap space into RAM.
3. Modern operating systems typically prefer which form of swapping?
Page-level swapping evicts individual pages on demand, which is far more granular and efficient than swapping whole processes.
Flash Cards
What is swapping? — Moving memory pages or processes between RAM and disk swap space to manage memory pressure.
Whole-process vs page-level swapping? — Whole-process moves an entire idle process; page-level (modern) evicts individual pages on demand.
What triggers a swap-in? — A page fault when a swapped-out page is accessed again.
What happens with too much swapping? — It degrades into thrashing, where the system spends most of its time swapping instead of computing.