Definition
Swapping: Moving entire process between main memory and secondary storage (disk)
Process swapped out: Entire context saved to disk, memory freed Process swapped in: Restored from disk to memory, execution resumes
Different from paging (paging moves pages, swapping moves entire process).
Swapping Concept
Basic Idea
Memory pressure: Too many processes, not enough RAM
Solution 1: Kill a process (bad!)
Solution 2: Swap to disk
Memory: 512MB, 10 processes × 60MB each = 600MB needed
Free space after loading 8 processes: 512 - (8×60) = 32MB
When 9th process arrives:
Swap out least recently used process (60MB freed)
Load 9th process (60MB allocated)
Memory still 512MB, 8 active + 1 waiting on disk
Swap Space
Area on disk designated for swapped processes:
Disk Layout:
┌──────────────────┐
│ File System │ (user files, OS)
├──────────────────┤
│ Swap Space │ (process images waiting to be loaded)
└──────────────────┘
Size: Typically 1-4x RAM
Location: Partition or dedicated partition
Access: OS managed, user invisible
Swap File
Single file (or multiple files) on disk:
/var/swap (Linux)
C:\pagefile.sys (Windows)
Typically hidden from user
Size: Varies as needed
Contents:
Swapped process image 1 (60MB)
Swapped process image 2 (50MB)
Swapped process image 3 (70MB)
Free space on disk
Swapping Process
Swap Out (Process → Disk)
Process in Memory:
┌─────────────────┐
│ Text (code) │
│ Data (globals) │
│ Stack │
│ Heap │
│ PCB │
│ Page Table │
└─────────────────┘
(Total: 60MB)
Decision: Swap out
Step 1: Save process state
- Register values to PCB
- Flush cache/TLB
Step 2: Write to disk
- All 60MB to swap file/partition
Step 3: Update OS tables
- Mark process as "swapped out"
- Free physical memory
Result:
Physical Memory: 60MB freed
Disk: Contains process image
Swap In (Disk → Process)
Swapped Process on Disk
Decision: Swap in (process selected to run)
Step 1: Find free memory
If not available, swap out another process
(Least recently used, similar to paging)
Step 2: Read from disk
- All process memory from disk (60MB)
- Load to physical memory
Step 3: Update OS tables
- Mark process as "in memory"
- Update page tables
- Invalidate TLB entries
Step 4: Restore state
- Restore registers from PCB
- Resume execution
Result:
Physical Memory: Process ready to run
Disk: Space available for other processes
Swapping vs Paging
| Feature | Swapping | Paging |
|---|---|---|
| Unit | Entire process | Single page (4KB) |
| Frequency | Infrequent (seconds) | Frequent (milliseconds) |
| Overhead | Very high (seconds!) | Moderate |
| Performance | Poor (process must wait) | Better (brief pause) |
| Flexibility | Less (whole process) | More (partial memory) |
| Disk Space | Large (entire process) | Small (one page) |
Swapping Scenarios
Scenario 1: Batch System
8:00 AM:
Memory full with long batch jobs
New job arrives
Least recently used job swapped to disk (30 seconds)
New job starts
Result: 30-second pause acceptable for batch system
Scenario 2: Interactive System
10:00 AM:
Memory full with interactive processes
User clicks mouse on background window
Process swapped in (2 seconds!)
Display freezes for 2 seconds
User experience: Bad! (system appears slow)
Why: Users expect < 100ms response
Swap Overhead
I/O Time
Process size: 60MB
Disk speed: 100 MB/s (modern SSD)
Swap out: 60MB / 100MB/s = 0.6 seconds
Swap in: 60MB / 100MB/s = 0.6 seconds
Total: 1.2 seconds!
Meanwhile:
CPU idle
Other processes wait
Disk is bottleneck
Context Switch Time
With swapping:
Process A running
Timer interrupt → switch to Process B
Without swapping:
1. Save Process A state (1ms)
2. Restore Process B state (1ms)
Total: 2ms
With swapping (if B swapped out):
1. Save Process A state (1ms)
2. Swap out Process A (600ms!)
3. Swap in Process B (600ms!)
4. Restore Process B state (1ms)
Total: 1202ms!
600x slower! System appears frozen!
When Is Swapping Useful?
1. Batch Systems (Legacy)
Jobs: 1GB each
Memory: 512MB
Three jobs waiting
Swap out lower priority job to disk
Run higher priority job
(Swap out finished job, swap in next)
2. Emergency Memory Management
System running well until:
User launches huge application
Unexpected memory spike
Swap mechanism as safety net
Otherwise system would crash
3. Embedded Systems (Rare)
Devices with tiny RAM (64MB)
Cannot page (insufficient RAM)
Swap entire processes to external storage
Acceptable because processes simple and small
Modern Perspective
Most modern systems DON’T swap:
Why:
1. Paging Is Better
- Move single pages (4KB) instead of 60MB
- Much faster (milliseconds vs seconds)
- CPU more responsive
2. More RAM Available
- Past: 64MB RAM was huge
- Now: 4GB-16GB common
- Rarely need to evict processes
3. SSDs Changed Dynamics
- SSD faster than spinning disk
- Still much slower than RAM (1000x)
- Paging to SSD acceptable, full process swap still slow
Modern Equivalents
Linux Swap:
- Still supports swapping
- Mostly disabled (disabled by default on modern systems)
- Used as backup if paging insufficient
Windows Pagefile:
- Hybrid approach (between paging and swapping)
- Swaps in memory pages to disk
- Called “paging” but operates on page level
macOS:
- Modern Mac OS uses page compression instead of swap
- Compresses inactive pages instead of swapping
- Faster than disk I/O
Performance Analysis
Scenario: Interactive System
System state:
- RAM: 4GB
- 20 applications running
- 5 applications active (foreground)
- 15 applications inactive (background)
With swapping:
Click background app → Swap in (1-2 seconds)
Result: Noticeable freeze, bad UX
With paging:
Click background app → Page in active pages (100ms)
Result: Barely perceptible, good UX
With RAM compression:
Click background app → Decompress pages
Result: Very fast, excellent UX
Evolution: Swapping → Paging → Compression
Deciding Swap vs Paging
| System Type | Strategy | Reason |
|---|---|---|
| Batch processing (old) | Swapping | Few processes, long-running jobs |
| Timesharing (old) | Paging | Many processes, frequent switching |
| Desktop (modern) | Paging + Compression | Interactive, responsive needed |
| Server (modern) | Paging or none | Sufficient RAM, no swap needed |
| Mobile (modern) | Compression | Limited memory, instant responsiveness |
Memory Pressure Scenarios
Scenario: Low Memory
Available: 200MB
Needed: 600MB
Solution without swap: Crash (OOM killer)
Solution with swap: Degrade gracefully (slow but running)
Scenario: High Memory
Available: 16GB
Needed: 12GB
Swap: Unused
System: Normal performance
Summary
Swapping moves entire processes to disk, useful for batch systems but poor for interactive use (seconds of delay). Paging moves individual pages (much faster). Modern systems prefer paging over swapping because disk I/O dominates performance, so minimizing I/O (paging 4KB vs swapping 60MB) crucial. Swapping mostly historical but still available as emergency mechanism. Understanding swapping important for appreciating why paging is superior. Modern systems use compression instead of swapping for better performance. Evolution: Swapping → Paging → Memory Compression shows how solutions improve over time.