Chapter 3 (English)
Topic wise chapter in English
4. Swapping
Swapping in Operating System
Swapping is a memory management technique in which a process is temporarily moved from main memory (RAM) to secondary storage (disk) and vice versa. This allows the operating system to manage limited RAM effectively and run multiple processes concurrently in a multiprogramming environment.
- Reduces memory fragmentation by moving processes in and out of RAM.
- Allows large processes to run even if they exceed available RAM.
- Frees RAM temporarily by suspending inactive or waiting processes.
- Supports virtual memory to give processes more memory than physically available.
Example: RAM has 4 GB with Process A and Process B running, each using 2 GB. When Process C, which also requires 2 GB, needs to run, there is no free memory. The operating system swaps Process A out to the disk and loads Process C into RAM. After Process C finishes, Process A is brought back into memory to continue execution.
How it Works:
- A process is moved into RAM from disk when it is ready to execute.
- After the process has run for a while (or if higher-priority processes need memory), it may be temporarily swapped out to disk.
- The CPU scheduler decides which process to swap in and out, often based on priority or scheduling policies.
- When the swapped-out process is needed again, it is swapped back into memory to continue execution.
Note: Swapping moves whole processes between RAM and disk, which is slow and inefficient. Modern OS like Windows, Linux, and macOS use paging instead of swapping . Paging loads only the needed parts of a process, making memory management faster and efficient.
| Advantages | Disadvantages |
|---|---|
| Reduces waiting time by using swap space as an extension of RAM, keeping the CPU busy. | Risk of data loss if the system crashes during swapping. |
| Frees main memory by moving inactive processes to secondary storage. | Increased page faults due to frequent swapping reduce performance. |
| Allows multiple processes to run using a single main memory with a swap partition. | Disk access is slower than RAM, causing execution delays. |
| Enables large programs to run on systems with limited physical memory. | Excessive swapping (thrashing) causes severe performance degradation. |
| Prevents memory overload by ensuring active processes have enough RAM. | CPU may spend more time swapping than executing processes. |