Thrashing
When the working set does not fit, the system spends its time paging, not computing.
Level: intermediate
What is it?
Thrashing is a state of severe memory pressure in which processes repeatedly fault on pages that are not resident, causing excessive paging and little useful CPU progress. It often appears when the combined active working sets exceed available physical memory.
Why does it matter?
Adding more runnable processes can lower system throughput if each loses locality and competes for too few frames. Recognizing thrashing shifts the response from adding CPU to controlling memory pressure and active concurrency.
Mental model
A program's working set is the set of pages it is actively reusing during a time window. If RAM cannot hold enough of those pages, the system repeatedly evicts a page that will soon be needed again.
How it works
- High page-fault rates and storage activity alongside poor useful throughput are warning signs.
- Working-set and page-fault-frequency approaches attempt to keep enough frames for active locality.
- Reducing multiprogramming or memory demand can restore progress.
- Not every page fault is thrashing; demand paging and cold-start faults are normal.
- Application memory leaks, oversized caches, and unbounded concurrency can contribute to pressure.
Steps
- Several processes each retain only part of their active working set.
- They repeatedly fault on evicted pages and trigger I/O to bring them back.
- The system spends more time paging and waiting than executing useful work.
- Reduce active memory demand, limit concurrency, or fix a workload that retains too much memory.
Real-world usage
- Diagnosing a host with high swap/page activity and low throughput.
- Reasoning about the balance between multiprogramming and available RAM.
- Designing services with bounded memory usage.
Common mistakes
- Assuming more processes always increase throughput.
- Treating a single page fault as proof of thrashing.
- Only adding CPU when the bottleneck is memory residency and storage I/O.
Interview questions
Why can increasing the number of processes reduce throughput?
Their combined working sets may no longer fit in RAM, causing repeated page faults and excessive paging overhead.
How can the OS respond to thrashing?
It can reduce multiprogramming or reclaim memory, and use working-set/page-fault signals to allocate enough frames to active workloads.