Thrashing and working set model in Operating Systems - Time & Space Complexity
When a computer runs many programs, it needs to manage memory efficiently. Thrashing happens when the system spends too much time swapping data in and out of memory instead of doing useful work.
We want to understand how the system's work changes as the number of programs or memory needs grow.
Analyze the time complexity of this simplified memory management loop.
while (true) {
for (each process) {
if (process needs page) {
if (page not in memory) {
swap page in;
}
}
}
}
This loop checks each process repeatedly to see if it needs pages in memory and swaps them in if missing.
Look at what repeats often in this code.
- Primary operation: Checking each process's page needs and swapping pages.
- How many times: The outer loop runs forever, and inside it, the system checks all processes repeatedly.
As the number of processes or pages grows, the system spends more time swapping pages.
| Input Size (number of processes) | Approx. Operations (page checks and swaps) |
|---|---|
| 10 | Moderate checks and occasional swaps |
| 100 | Many checks and frequent swaps, slowing work |
| 1000 | Very high checks and constant swapping, system thrashes |
Pattern observation: As processes increase, the system spends more time swapping pages, which grows quickly and hurts performance.
Time Complexity: O(n)
This means the time spent checking and swapping grows roughly in direct proportion to the number of processes.
[X] Wrong: "Adding more memory always stops thrashing immediately."
[OK] Correct: Sometimes, even with more memory, if the working set (pages needed) is too large or poorly managed, thrashing can still happen.
Understanding how system performance changes with more processes and memory needs helps you explain real-world computer behavior clearly and confidently.
What if the system used a better working set model that predicts page needs more accurately? How would the time complexity change?