Why Your Operating System Freezes: The Scheduler and Memory Root Causes
Archi4 min read
Contents
- The freeze is usually a queue, not a crash
- Run queues and the illusion of parallelism
- Priority inversion, in plain terms
- Memory pressure and the page fault storm
- Overcommit and the out-of-memory killer
- Lock contention and the scheduler's own locks
- Reading the evidence instead of guessing
- What actually helps
The freeze is usually a queue, not a crash#
When a computer stops responding, people say it crashed. Most of the time it did not. The kernel is still running, the fans are still spinning, and the disk light may even be blinking. What stopped is your ability to get work done, because every process you care about is waiting behind something else. Finding the root cause means finding out what they are waiting for.
Operating systems schedule work in two main ways: they hand the CPU to runnable threads, and they hand out memory pages on demand. Almost every freeze traces back to one of those two machinery rooms, or to a lock that guards them.
Run queues and the illusion of parallelism#
A modern laptop may have 8, 12 or 16 hardware threads. The OS keeps a run queue for each core. A thread that is ready to run sits in a queue until a core is free. When the number of runnable threads exceeds the number of cores, latency climbs. That is normal under load.
The trouble starts when a thread holds a core without yielding. A driver spinning in a loop, a process stuck in an uninterruptible disk wait, or a scheduler bug that fails to preempt can pin a core for seconds. Do that on a 4 core machine and you have lost 25 percent of the machine. Do it on a phone with 2 big cores and most of the interface stops painting.
This is why a single misbehaving driver can freeze a whole desktop while the CPU graph still shows activity. The cores are busy. They are just busy on the wrong thing.
Priority inversion, in plain terms#
Suppose a low priority background task holds a lock that a high priority task needs. The high priority task waits. A medium priority task, which needs no lock at all, keeps running and keeps the low priority task from finishing. The high priority task is now effectively slower than the medium one. This is priority inversion, and it has frozen spacecraft and consumer routers alike. Real time schedulers fix it with priority inheritance: the low priority holder temporarily borrows the high priority so it can finish and release the lock.
Memory pressure and the page fault storm#
Memory is handed out lazily. When a program asks for 1 GB, the kernel often just writes down the promise. Physical pages arrive later, on first touch, through a page fault. That design keeps startup fast. It also means a program can look healthy while holding almost no real memory.
When physical memory runs short, the kernel reclaims pages. Clean file pages can be dropped and read again from disk. Dirty pages must be written first. Anonymous pages (heap and stack) that belong to running programs can only be freed by swapping them out. If the swap device is slow, or the working set is larger than RAM, the system enters a loop: fault, reclaim, fault again. Throughput collapses even though the CPU is not pegged.
This is the classic thrash. The machine is not dead. It is doing useful work at a rate of a few pages per second while every click waits in line.
Overcommit and the out-of-memory killer#
Linux and several other kernels overcommit memory by default. A process can reserve more address space than the machine has. That is fine until many processes touch their reservations at once. Then the kernel must choose a victim. The out of memory killer picks a process, often a large one, and terminates it without warning. Users see an app vanish and assume a bug in the app. The cause is a global accounting decision made milliseconds earlier.
Lock contention and the scheduler's own locks#
Every scheduler run is itself a shared data structure. On many core systems the run queue lock becomes a hot spot. Profiling tools show time spent in functions with names like schedule and try_to_wake_up. When that lock is contended, even a trivial wake up takes longer than it should, and wake up latency is exactly what users feel as stutter.
Filesystem locks cause the same pattern. A process writing a large log file can hold a lock that a process trying to read a directory needs. The reader blocks. If the reader is the file manager redrawing your window, the desktop appears frozen while the disk grinds.
Reading the evidence instead of guessing#
On Linux, start with vmstat 1 and watch the r column (runnable threads) and the si/so columns (swap in and out). If r is far above your core count and si/so are nonzero, you have a queue and memory problem, not a CPU problem. pidstat -w shows context switches per process. perf top reveals which kernel functions are hot. A stack trace showing many threads blocked on the same mutex is a direct pointer to the holder.
On macOS, top and the Activity Monitor memory pressure graph serve the same purpose. On Windows, Resource Monitor's commit charge and hard fault rate tell the same story.
What actually helps#
The fixes follow from the root cause. If swap is the bottleneck, add RAM or move swap to faster storage, and cap the memory of the worst offenders. If a driver spins, update or remove it. If a filesystem lock is the culprit, separate the noisy writer onto its own volume. If priority inversion appears in your own code, do not hold a lock across a long operation.
None of this requires exotic tools. It requires reading the run queue and the page fault counters before assuming the machine is broken. Most freezes are the OS doing exactly what it was told, with more work than the hardware can absorb.
More articles