Your laptop has plenty of tabs open and is fine. You open one more, and suddenly everything freezes: the mouse stutters, the disk light stays on, and nothing seems to get done. The CPU, strangely, is mostly idle. What did one more tab do?
This is thrashing. The system is so short of memory that it spends nearly all its time moving pages between RAM and disk, and almost none running your programs.
The working set
A program does not use all its pages all the time. At any moment it uses a small group: the code of the loop it is in, the array it is scanning, the top of the stack. This group of pages in active use is its working set. If the working set is in RAM, the program runs at full speed, with few faults. If part of it is missing, the program faults again and again, because the pages it needs keep getting evicted to make room for the others it also needs.
So memory is not “enough” in total: it must be enough for the working sets of all the programs that are running. As long as the working sets add up to no more than RAM, every program has what it needs.
Adding processes one at a time
The simulator below is a deliberately simple model. RAM is 100 frames, and each process needs a working set of 20 frames. It adds seven processes one at a time. The bar on top is RAM, filled by the working sets, and the chart tracks CPU utilisation as processes are added.
Two effects are at work. While one process waits for ordinary I/O, others can use the CPU, so more processes keep the CPU busier. But once the working sets no longer fit, a fraction of the pages every process needs is missing, and every process waits for the disk. The simulator combines the two in a simple formula: the busier-with-more-processes effect, divided down by how much of the needed memory is missing. The numbers belong to this model and not to any real machine, but the shape is real.
Adding processes until RAM runs out
-
Add A (needs 20)
There are 1 process and together their working sets need 20 frames of the 100. Everything fits, so there are no page faults. One process alone leaves the CPU idle whenever it waits for I/O: 40%.
- Processes
- 1
- Demand (working sets)
- 20 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 40%
- Best so far
- 40% with 1
-
Add B (needs 20)
There are 2 processes and together their working sets need 40 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 64%.
- Processes
- 2
- Demand (working sets)
- 40 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 64%
- Best so far
- 64% with 2
-
Add C (needs 20)
There are 3 processes and together their working sets need 60 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 78%.
- Processes
- 3
- Demand (working sets)
- 60 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 78%
- Best so far
- 78% with 3
-
Add D (needs 20)
There are 4 processes and together their working sets need 80 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 87%.
- Processes
- 4
- Demand (working sets)
- 80 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 87%
- Best so far
- 87% with 4
-
Add E (needs 20)
There are 5 processes and together their working sets need 100 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 92%. Five processes need exactly 100 frames, all that RAM has. The CPU is as busy as it gets.
- Processes
- 5
- Demand (working sets)
- 100 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 92%
- Best so far
- 92% with 5
-
Add F (needs 20)
Now the working sets need 120 frames and RAM has only 100. About 17% of the pages the processes need are not in RAM. They keep faulting and each fault waits for the disk, and the pages loaded for one process push out pages another needs. CPU utilisation falls to 12%: this is thrashing.
- Processes
- 6
- Demand (working sets)
- 120 of 100 frames
- Pages missing
- 17%
- CPU utilisation
- 12%
- Best so far
- 92% with 5
-
Add G (needs 20)
Demand is 140 frames against 100. 29% of the needed pages are missing and utilisation is 8%. The system spends its time moving pages between RAM and disk, not running programs.
- Processes
- 7
- Demand (working sets)
- 140 of 100 frames
- Pages missing
- 29%
- CPU utilisation
- 8%
- Best so far
- 92% with 5
The chart rises while processes are added, then falls off a cliff at the sixth, from 92% to 12% in this model. Look at the seventh: utilisation falls further. Every process added to a system that is already thrashing makes it worse.
Here is the vicious circle that makes thrashing hard to escape. The CPU is idle because everyone is waiting for the disk. A naive scheduler sees an idle CPU and decides to start more processes to keep it busy. Those need more memory, which causes more faults, which makes the CPU idler. The system is not just slow, it is spiralling.
Getting out
Open Try it with these processes and try each of the fixes in the model:
- Fewer processes. Remove processes from the list until the total of the working sets is no more than RAM again. The cliff disappears.
- More RAM. Raise the RAM number and run again: the cliff moves to the right, to a larger number of processes.
- Kill something. When there is truly no way to fit the working sets, the OS has to free memory by ending a process. On Linux this is the OOM killer (out-of-memory killer): it picks a process, ends it, and takes its frames back. The next simulator has the OOM killer switched on. Four processes of 30 frames fit in 100 frames only three at a time.
The OOM killer
-
Add A (needs 30)
There are 1 process and together their working sets need 30 frames of the 100. Everything fits, so there are no page faults. One process alone leaves the CPU idle whenever it waits for I/O: 40%.
- Processes
- 1
- Demand (working sets)
- 30 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 40%
- Best so far
- 40% with 1
-
Add B (needs 30)
There are 2 processes and together their working sets need 60 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 64%.
- Processes
- 2
- Demand (working sets)
- 60 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 64%
- Best so far
- 64% with 2
-
Add C (needs 30)
There are 3 processes and together their working sets need 90 frames of the 100. Everything fits, so there are no page faults. More processes mean the CPU almost always has something to run while others wait for I/O, so utilisation rises to 78%.
- Processes
- 3
- Demand (working sets)
- 90 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 78%
- Best so far
- 78% with 3
-
Add D (needs 30)
Now the working sets need 120 frames and RAM has only 100. About 17% of the pages the processes need are not in RAM. They keep faulting and each fault waits for the disk, and the pages loaded for one process push out pages another needs. CPU utilisation falls to 11%: this is thrashing. The fourth process needs 30 more frames, and only 10 are left. The system starts to thrash.
- Processes
- 4
- Demand (working sets)
- 120 of 100 frames
- Pages missing
- 17%
- CPU utilisation
- 11%
- Best so far
- 78% with 3
-
The OOM killer kills A
Memory is far too short, so the kernel picks the process using the most memory, A (30 frames), and kills it to get its frames back. Demand falls to 90 frames, RAM is enough again, and utilisation recovers to 78%. One process was lost so that the others can run.
- Processes
- 3
- Demand (working sets)
- 90 of 100 frames
- Pages missing
- 0%
- CPU utilisation
- 78%
- Best so far
- 78% with 3
Killing a process feels brutal, but the alternative is a system so slow that nothing finishes, which is worse for everyone. The OOM killer is the last resort.
In the real world
When the working sets do not fit, Linux can move rarely used pages to a swap area on disk, which makes room. This is why the machine does not simply stop when RAM is full. It is also why a machine that is using swap heavily is thrashing: a page that is swapped out must be read back in before the program can use it.
The command vmstat 1 prints one line of numbers every second. Two columns tell the story: si (memory swapped in from disk) and so (memory swapped out to disk). A healthy machine shows zero or near zero in both. A thrashing machine shows large values in both, together with a high wa column (CPU time spent waiting for I/O) and a low us (user time).
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
If memory runs out even with swap, the kernel’s OOM killer picks a victim and writes a line like this in the kernel log (you can read it with dmesg):
Out of memory: Killed process <pid> (<name>) total-vm:... anon-rss:...
The process id, the name and the sizes are different on every machine and every incident. When a server’s program disappears with no error of its own, this line is often the reason. Containers can have their own memory limits, and the same thing happens inside them (Module 8). :::
Quick check
Quick check
-
A program uses a small part of its memory at a time. If that part is in RAM it runs well; the rest can wait on disk.
-
Thrashing is a shortage of memory, not of CPU. The system is busy moving pages, so little useful work is done.
-
The extra working sets push out more pages that are needed, so the faults increase. Fewer processes or more RAM are the real fixes.
-
It is the last resort when memory cannot be freed any other way.
Review: what you learned earlier
Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.
Spaced review
This review picks questions for you, so it needs JavaScript.
Module 6 recap
You can now follow an address from a program down to RAM. Without any translation, programs clash, and base and bounds registers give each one a private block. Segments split that block into parts, but variable-sized pieces leave holes (external fragmentation), which first fit, best fit and compaction only manage. Paging cuts memory into equal pages and frames, splits each address into a page number and an offset, and uses a page table and the MMU to translate. The TLB makes translation fast, and multi-level tables make it small. Demand paging loads pages only when a page fault asks for them, and copy-on-write makes fork cheap. When RAM is full, FIFO, LRU, OPT and Clock decide which page goes (and Belady’s anomaly shows that more frames can mean more faults under FIFO). When the working sets of the running programs do not fit in RAM, the system thrashes.
Next, Module 7: programs also need to keep data after they stop. You will see how devices talk to the CPU, how a disk head moves, and how files and journals are laid out.
Join the discussion