Thrashing & Working Set

RoadmapsCore CS

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

Common mistakes

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

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

Return to Core CS Roadmap