Operating Systems · Module 7 — Virtual Memory
Thrashing and the working set
Thrashing is when a system spends more time swapping pages than executing instructions.
Sign in to track your score
Aisha opens more tabs. Then a video call. Then a second compile.
At first everything gets a bit slower, which is expected.
Then it falls off a cliff. The mouse pointer stutters. Nothing responds. The SSD light is on constantly.
And CPU usage has dropped to almost nothing. The machine is doing less work than it was an hour ago, while looking far busier.
Why & what
Thrashing is when a system spends more time swapping pages than executing instructions.
How it starts. Each process has some set of pages it needs right now. Load enough processes and no process has room for its own set. Every process faults, constantly, on pages it just gave up.
The death spiral. This is the part worth understanding properly.
- Processes fault constantly, so they are all in Waiting.
- CPU utilisation drops, because nobody is ready to run.
- The OS sees an idle CPU and concludes not enough processes are loaded.
- So it loads more processes.
- Which makes memory pressure worse. Which increases faulting.
The OS's own attempt to be efficient makes the collapse steeper. That is why the curve does not level off — it falls.
The working set. The fix comes from measuring what a process actually needs.
The working set of a process is the set of distinct pages it referenced in the last Δ references, where Δ is a window such as the last 10,000 accesses.
If the last six references were 3, 2, 5, 5, 6, 5, the working set is {2, 3, 5, 6} — four distinct pages, so four frames.
The working set rule. Add up the working sets of all processes. If the total exceeds the number of frames available, the system will thrash. So:
- Give every process at least its working set in frames.
- If there are not enough frames for everyone, suspend a process entirely and swap it out.
Suspending one process so the rest can run properly sounds harsh and is correct. Four processes running well beat six processes thrashing.
Page fault frequency, the simpler alternative. Working sets are fiddly to measure. A cruder scheme works nearly as well: watch each process's fault rate directly and set an acceptable band. Too high, and the process needs more frames — give it some. Too low, and it has more than it needs — take some away. If the fault rate is too high and there are no frames to give, suspend something. Same outcome, far less bookkeeping.
Why this connects to locality. The working set is only a small fraction of a process's pages because of locality of reference — the same principle that made the TLB work in Topic 6.4. A process working through a loop touches a handful of pages for a long time, then moves to a different handful. Between those shifts, its working set is small and stable.
How it works
- The OS tracks which pages each process touched recently, using the reference bits sampled periodically.
- It computes each process's working set size — the count of distinct pages in the window.
- It sums them. That is the total demand for frames.
- If demand exceeds supply, it suspends a process and swaps it out completely, freeing all its frames at once.
- When pressure drops, the suspended process is brought back. Those are the "suspended" states Topic 2.2 mentioned and deferred.

Common confusion
"Thrashing means running out of memory." It means running out of memory for the working sets. The Nova-14 might have plenty of total RAM while still thrashing, if it is split too many ways for any one process to hold what it needs. Total capacity is not the measure; per-process sufficiency is.
"High CPU usage means thrashing." The opposite. During thrashing the CPU is nearly idle, because every process is blocked waiting on the disk. High disk activity plus low CPU usage plus an unresponsive machine is the signature.
"Adding swap space fixes thrashing." It makes it worse. More swap means more processes can be admitted, which is exactly the wrong direction. The fixes are more RAM, or fewer processes.
Interview angle
"What is thrashing and why does it happen?" Define it as more time spent swapping than executing, then describe the spiral: faults rise, CPU utilisation drops, the OS loads more processes, faults rise further. The feedback loop is what the question is really testing.
"What is the working set?" The distinct pages referenced in a recent window. Then state the rule it exists for: if the sum of all working sets exceeds available frames, the system will thrash. "How do you fix thrashing?" Two real answers: give processes their working sets and suspend those that cannot be satisfied, or reduce the degree of multiprogramming. Adding swap space is the wrong answer, and interviewers do sometimes offer it to see whether you take it.
- 1.
During thrashing, what does CPU utilisation look like?
- 2.
The last six references were 3, 2, 5, 5, 6, 5. What is the working set for a window of 6?
- 3.
A machine is thrashing. Which action would make it worse?