The 45-Second Freeze: Tracing a Desktop Hang to Its Kernel Root
Archi4 min read
Contents
The Freeze That Leaves No Crash Log#
A desktop that locks for 30 to 60 seconds and then recovers is one of the hardest bugs to file. Nothing panics. No core dump lands in /var/crash. The machine simply stops responding to input, then resumes as if nothing happened, often while a video keeps playing in a browser window that was muted anyway.
Over three weeks I reproduced this on three machines: a 2019 ThinkPad with 16 GB of RAM and an NVMe drive, a desktop with 32 GB and a SATA SSD, and a budget laptop with 8 GB and a slow eMMC disk. All ran current Linux kernels, one ran Windows 11 for comparison. The trigger in every case was the same class of workload: a browser with 40 or more tabs, a container runtime, and a large file copy running at the same time.
The short version: the hang is almost never the scheduler. It is memory reclaim, specifically what happens when the kernel runs out of clean page cache and has to write dirty pages back to disk while a process is waiting on them.
What the logs actually showed#
On the Linux machines I enabled kernel.sysrq and used the magic SysRq task dump during a hang. The pattern repeated: several kworker threads sat in D state (uninterruptible sleep) inside balance_dirty_pages, while the desktop compositor waited on a memory allocation that could not be satisfied until writeback finished.
On the 8 GB laptop the stall stretched to 90 seconds. Its eMMC disk sustained roughly 40 MB/s of random writes, so flushing a few hundred megabytes of dirty page cache took far longer than on the NVMe machine, where the same workload produced a 12-second freeze.
That ratio is the whole story. The kernel throttles processes that dirty pages once the dirty ratio crosses a threshold. If the storage underneath is slow, the throttle becomes a visible freeze.
Why the Scheduler Gets Blamed and Is Usually Innocent#
The first instinct is to blame CPU scheduling, because the symptom looks like starvation. It rarely is. A scheduler problem shows up as high load averages with processes still making progress, and usually as latency spikes measured in milliseconds, not tens of seconds.
When a task sits in D state, the scheduler has nothing to run. The task is waiting on I/O, not competing for a core. Checking ps -eo state,comm | grep '^D' during a hang settles the question in seconds. On all three test machines, the frozen tasks were in D, not R.
The three settings that move the needle#
I tested each of these in isolation with a scripted workload: open 60 tabs in a Chromium build, start a docker build, and copy a 4 GB file.
, vm.dirty_ratio defaulted to 20 percent of RAM. On the 8 GB machine that allowed about 1.6 GB of dirty pages before hard throttling. Dropping it to 10 percent cut the worst freeze from 90 seconds to 41., vm.dirty_background_ratio at 10 percent meant background writeback started late. Setting it to 5 percent began flushing sooner and smoothed the stall., vm.swappiness at 60 pushed anonymous pages toward swap aggressively. With zram enabled the effect was mixed: the freeze shortened, but total throughput dropped about 8 percent on the budget laptop.
On Windows 11 the equivalent symptom appeared as a stalled Explorer window during a large copy. Resource Monitor showed disk queue depth above 100 and system process CPU near zero, which is the same signature: waiting on storage, not compute.
Memory Pressure Is the Real Trigger#
Page cache is not free memory that happens to hold files. It is memory the kernel will reclaim under pressure, and reclaiming a dirty page means a disk write. When every page the kernel tries to reclaim is dirty, allocation stalls until the write completes.
The clearest confirmation came from watching /proc/vmstat. During each hang, pgscan_direct and pgsteal_direct spiked while pgrotated stayed flat. Direct reclaim means the allocating process did the work itself, blocking until it finished. That is the freeze, rendered in counters.
A second confirmation: on the 32 GB desktop, I artificially capped the container runtime at 2 GB with cgroup limits. The freeze disappeared entirely at the same tab count, because memory pressure never reached the point where direct reclaim kicked in.
The fix that worked, ranked by effort#
- Enable zram swap at 50 percent of RAM. This absorbed spikes without touching disk on two of the three machines.
- Lower dirty ratios as described above. Cheap, reversible, and the single biggest improvement on the eMMC laptop.
- Add a small amount of fast swap on the NVMe machine rather than disable swap entirely. Machines with swap off froze harder when pressure hit, because reclaim had nowhere to send anonymous pages.
- Move the heavy write workload to a separate cgroup with an I/O weight limit, so foreground interaction kept priority.
None of these eliminate the underlying behavior. They change how often memory pressure reaches the point where a process must wait on its own disk writes. The 45-second freeze is not a mystery once you know where to look: not the CPU, not the compositor, but the reclaim path doing exactly what it was told.
More articles