1. Introduction
Main memory (RAM) is a limited resource, while modern operating systems must support the execution of multiple processes simultaneously. As the number of active processes increases, available memory may become insufficient to accommodate all of them.
To address this limitation, operating systems use a technique called swapping, which temporarily moves processes between main memory and secondary storage.
Swapping is a memory management technique that enables multiprogramming by transferring processes between RAM and disk whenever memory space is needed.
2. What is Swapping?
Swapping is a memory management technique in which an entire process is temporarily transferred from main memory (RAM) to secondary storage (disk) and later brought back into memory for continued execution.
The process moved out of memory is called a swapped-out process, while the process brought back into memory is called a swapped-in process.
Key Idea
RAM ↔ Swap Space (Disk)
A process does not remain permanently in RAM. When memory becomes scarce, the operating system may move some processes to disk and reload them later when required.
3. Why Swapping is Needed
3.1 Limited Main Memory
RAM has limited capacity.
As more processes execute simultaneously, memory may become insufficient to hold all active processes.
Swapping helps create additional space by temporarily moving some processes to disk.
3.2 Support for Multiprogramming
Multiprogramming requires multiple processes to reside in memory.
Swapping allows more processes to participate in execution than can physically fit in RAM at one time.
3.3 Better CPU Utilization
When a process is waiting for I/O or remains inactive for a long period, the operating system can swap it out and allocate memory to a more active process.
This improves overall system utilization.
3.4 Increased Degree of Multiprogramming
Swapping enables the system to maintain a larger number of processes, thereby increasing the degree of multiprogramming.
4. Working of Swapping
Swapping involves moving processes between RAM and disk whenever memory management decisions require it.
Step-by-Step Process
Step 1
Several processes are loaded in RAM.
RAM:
P1 P2 P3
Step 2
A new process arrives, but memory is full.
RAM:
P1 P2 P3
New Process:
P4
Step 3
The operating system selects a process to remove.
Suppose P1 is selected.
Step 4
P1 is moved to swap space on disk.
Disk:
P1
Step 5
The freed memory space is allocated to P4.
RAM:
P2 P3 P4
Step 6
Later, when P1 must execute again, it is brought back into memory.
Disk → RAM
P1
Execution then continues from the point where it was suspended.
5. Terminology
Swap Out
Moving a process from RAM to disk.
RAM → Disk
Also called roll out.
Swap In
Moving a process from disk back to RAM.
Disk → RAM
Also called roll in.
6. Swap Space
Swapping requires a dedicated storage area on disk known as swap space.
Definition
Swap space is a reserved area of secondary storage used to hold processes that have been temporarily removed from memory.
Characteristics
Located on disk
Larger than RAM
Much slower than RAM
Managed by the operating system
Purpose
Provides temporary storage for inactive or swapped-out processes.
RAM
↓
Swap Space
↓
RAM
7. Process Relocation During Swapping
A process may not return to the same physical memory location from which it was removed.
For example:
Before Swapping:
P1 → Address 1000
After swapping back:
P1 → Address 5000
Therefore, the operating system must support relocation, allowing a process to execute correctly regardless of where it is loaded.
This is achieved through address translation mechanisms such as:
Base and limit registers
Paging
Segmentation
8. Example Scenario
Assume RAM can hold only three processes.
Initial State
RAM:
P1 P2 P3
A new process P4 arrives.
Swap Out
The operating system chooses P2.
Disk:
P2
Load New Process
RAM:
P1 P3 P4
Later, when P2 is needed again:
Disk → RAM
P2 returns
This allows all processes to make progress despite limited memory.
9. Performance Considerations
Although swapping improves memory utilization, it introduces overhead because disk operations are much slower than memory operations.
9.1 Swap Time
Swap time depends primarily on:
Process size
Disk transfer speed
A larger process requires more time to transfer.
Approximation
Swap Time
=
Transfer Time Out
+
Transfer Time In
Since disk access is slow, swap time can be significant.
9.2 Context Switching Overhead
Swapping increases the effective cost of context switching because the operating system may need to move process data between memory and disk.
9.3 Disk Bottleneck
Heavy swapping places additional load on secondary storage.
This can become a major performance bottleneck.
10. Thrashing (Very Important)
Definition
Thrashing occurs when the operating system spends more time swapping processes between RAM and disk than executing actual instructions.
Why It Happens
When too many processes compete for insufficient memory:
Processes are repeatedly swapped out
Processes are repeatedly swapped in
CPU waits for disk operations
Conceptual Representation
Swap Out
Swap In
Swap Out
Swap In
Swap Out
Swap In
Very little useful computation occurs.
Effects
Extremely poor performance
High disk activity
Low CPU utilization
Long response times
Key Insight
Thrashing is a symptom of excessive multiprogramming combined with insufficient memory.
11. Advantages of Swapping
Supports Multiprogramming
More processes can be managed than can physically fit in RAM.
Better Memory Utilization
Memory can be allocated dynamically to active processes.
Increased Flexibility
Inactive processes can be temporarily removed.
Improved Resource Usage
Allows memory to be used by processes that currently need it most.
12. Disadvantages of Swapping
High Overhead
Disk operations are significantly slower than memory operations.
Performance Degradation
Frequent swapping increases execution time.
Thrashing Risk
Excessive swapping may severely reduce system performance.
Additional Disk Requirement
Dedicated swap space is required.
13. Swapping vs Paging
| Feature | Swapping | Paging |
|---|---|---|
| Unit of Transfer | Entire Process | Fixed-Size Pages |
| Granularity | Coarse | Fine |
| Memory Utilization | Lower | Higher |
| Flexibility | Low | High |
| Performance | Slower | Faster |
| Modern Usage | Limited | Extensive |
Key Difference
Swapping transfers the entire process:
Process ↔ Disk
Paging transfers only required pages:
Page ↔ Disk
Paging is therefore more efficient and forms the basis of modern virtual memory systems.
14. Relationship with CPU Scheduling
Swapping often works together with process scheduling.
For example:
Scheduler selects a process.
Process is not present in RAM.
Operating system swaps it in.
Process begins execution.
Thus, memory management and process scheduling are closely connected.
15. Real-World Analogy
Imagine a student studying at a desk.
Components
Desk = RAM
Storage cabinet = Disk
Books = Processes
Situation
The desk can hold only a few books.
When a new book is needed:
Move old book to cabinet
Bring new book to desk
Continue studying
Later, if the old book is needed again:
Retrieve it from cabinet
Place it back on desk
This is exactly how swapping works.
Most Important Point
Swapping improves memory utilization by temporarily moving entire processes between RAM and disk, but excessive swapping can lead to thrashing and severe performance degradation.