Thrashing in Operating Systems
Thrashing in Operating Systems
What Is Thrashing?
Thrashing occurs when a system spends more time swapping pages in and out of memory than executing actual programs.
In simple terms:
The CPU is mostly waiting for disk I/O instead of doing useful work.
Why Does Thrashing Happen?
Thrashing happens when:
-
Too many processes are running
-
Total memory demand exceeds physical RAM
-
The system experiences very high page fault rates
When this happens:
-
Pages are constantly evicted
-
Recently evicted pages are needed again
-
The system enters a loop of continuous page faults
How Thrashing Develops (Step-by-Step)
-
System runs many processes.
-
Each process needs several pages.
-
Physical memory becomes full.
-
Page replacement begins.
-
A process references a page that was just evicted.
-
Another page fault occurs.
-
Another page gets evicted.
-
The cycle repeats.
Eventually:
The CPU is idle, waiting on disk most of the time.
Example 1: Simple Numerical Example
Suppose:
-
Physical memory = 4 frames
-
Process needs 6 pages
-
Replacement policy = FIFO
Reference string:
What happens?
-
First 4 pages fill memory
-
When page 5 is accessed → page 1 is removed
-
Next reference is 1 → page fault again
-
Page 2 gets removed
-
Next reference is 2 → page fault again
Result:
👉 Almost every access causes a page fault
👉 System constantly swaps pages
👉 This is thrashing
Example 2: Multiple Processes
Suppose:
-
RAM can hold 100 pages
-
5 processes running
-
Each needs 30 pages to run efficiently
Total needed = 150 pages
Available = 100 pages
System cannot satisfy working sets.
What happens?
-
Pages constantly evicted
-
Processes never stabilize
-
Page fault rate skyrockets
-
CPU utilization drops
The OS might incorrectly think:
“CPU is idle, let's add more processes!”
This makes it worse.
Symptoms of Thrashing
| Symptom | What Happens |
|---|---|
| High page fault rate | Constant disk access |
| Low CPU utilization | CPU waiting on disk |
| High disk activity | Swap space heavily used |
| Slow program performance | System feels frozen |
Working Set Concept
Each process has a working set:
The set of pages actively used during a time period.
Thrashing occurs when:
If working sets fit → no thrashing
If they don’t → thrashing begins
Why Thrashing Is Dangerous
Disk is much slower than RAM:
| Component | Access Time |
|---|---|
| RAM | Nanoseconds |
| Disk | Milliseconds |
That’s 10,000–100,000x slower.
Even simple instructions can take milliseconds.
How Operating Systems Prevent Thrashing
1️⃣ Working Set Model
OS tracks each process's working set.
-
Only run processes whose working sets fit in RAM.
2️⃣ Page Fault Frequency (PFF)
OS monitors page fault rate:
-
If too high → reduce multiprogramming level
-
If low → may admit more processes
3️⃣ Reduce Multiprogramming
Suspend or swap out entire processes.
Fewer processes = less competition for memory.
4️⃣ Better Page Replacement Policies
Algorithms like:
-
LRU (Least Recently Used)
-
Clock algorithm
Help reduce unnecessary evictions.
Key Insight
Thrashing is not just “many page faults.”
It is:
A condition where the system becomes dominated by paging activity instead of computation.
Final Takeaway
Thrashing occurs when:
It causes:
-
Massive performance degradation
-
High disk I/O
-
Low CPU utilization
And it is one of the most important performance problems in virtual memory systems.
Comments
Post a Comment