Thrashing & Working Set
Scenario
Have you ever opened 200 Chrome tabs and watched your computer freeze entirely? The hard drive light blinks furiously, but nothing happens on screen. You've just witnessed thrashing.
Mental model
It's like trying to juggle 10 balls when you only have the skill for 3. You spend 100% of your time picking up dropped balls instead of actually juggling.
Thrashing occurs when a system spends more time swapping pages in and out of disk than executing actual CPU instructions, bringing the system to a complete halt.
Explanation
Modern operating systems try to maximize CPU utilization by keeping many processes in memory simultaneously. If one process pauses for I/O, the CPU can instantly switch to another. However, if the OS loads too many processes, the total amount of memory actively needed by all these processes will exceed the available physical RAM. When this happens, a vicious cycle begins.
A process experiences a page fault and requests a page from disk. Because RAM is completely full, the OS must evict a page belonging to another process. But that other process actually needed that page, so as soon as it gets CPU time, it immediately page faults and kicks out a different page. The system enters a state of continuous, compounding page faults. Since accessing the disk takes milliseconds (eternities in CPU time), the CPU spends all its time waiting for the disk. CPU utilization drops to near zero, making the system feel completely frozen. This catastrophic state is called thrashing.
To prevent thrashing, the OS must understand how much memory a process actually needs to function smoothly at any given moment. This is defined as the 'Working Set'. A process's working set is the set of pages it has actively referenced in the recent past. Due to locality of reference, a process only needs its working set in RAM to run without frequent page faults; it does not need its entire memory footprint loaded.
The OS actively monitors the size of the working set for every running process. If the sum of all working sets exceeds total physical memory, thrashing is imminent. To recover, the OS takes drastic action: it suspends one or more entire processes, swapping them completely to disk. This frees up enough frames so that the remaining active processes have enough room for their working sets, allowing them to finish. Once they finish, the suspended processes can be brought back in.
Key points
- CPU Utilization Drop: The hallmark symptom of thrashing is a sudden, massive drop in CPU utilization combined with a massive spike in disk I/O activity.
- Working Set Model: The working set is a dynamic window. As a program shifts from initialization to core processing to cleanup, its working set changes in both size and contained pages.
- Local vs Global Replacement: Global page replacement (allowing a process to steal frames from *any* other process) drastically increases the risk of system-wide thrashing compared to local replacement.
Common mistakes
- Thinking thrashing is a CPU bottleneck: This is a common source of confusion.
- Assuming more multiprogramming always equals more performance: This is a common source of confusion.
Glossary
- CPU utilization
- A measure of how busy and productive the computer's main processor is kept, aiming to avoid sitting idle.
- vicious cycle
- A disastrous loop where the computer spends so much time swapping memory to the hard drive that it gets no actual work done, causing it to freeze up.
- physical RAM
- The actual, fast memory chips inside the computer that have a strict, limited capacity.
Recall questions
- What is thrashing in the context of Operating Systems?
- What causes the sharp drop in CPU utilization during thrashing?
- How does the Working Set model prevent thrashing?
Questions & answers
If a system is thrashing, which hardware component is the primary bottleneck?
The secondary storage (Hard Disk / SSD).
Approach: Thrashing is characterized by excessive disk I/O as pages are constantly swapped in and out. The CPU is idle.
How can an OS recover from a severe thrashing state?
By decreasing the degree of multiprogramming (suspending or killing some running processes).
Approach: Thrashing means total working sets > RAM. The only immediate fix is to remove some working sets entirely from the equation by suspending processes.
Continue learning
Previous: Demand Paging & Page Faults