</>EmbeddedInterview

What is thrashing?

EasyOperating System Concepts

Short answer: Thrashing is when the combined working sets of the running processes do not fit in physical memory, so the system spends most of its time moving pages between RAM and swap (or re-reading evicted file pages) instead of doing useful work. CPU utilisation drops, disk I/O is saturated, and everything becomes extremely slow.

The feedback loop that makes it worse: low CPU utilisation (everyone is waiting for page-ins) can trick a naive OS or administrator into starting more work, which increases memory pressure further.

Symptoms on Linux

  • vmstat 1: high si/so (swap in/out) and wa (I/O wait), low us/sy.
  • /proc/pressure/memory (PSI): high "some" and "full" percentages.
  • Major page faults rising in sar -B or perf stat.

Working set: the set of pages a process has used recently. If the sum of all working sets is larger than RAM, thrashing follows no matter how good the replacement algorithm is.

Fixes

  • Add RAM, or reduce memory use (fewer processes, smaller caches, fix leaks).
  • Limit memory per service with cgroups (memory.max, memory.high) so one runaway process cannot drag the system down.
  • Let the OOM killer or a user-space early-OOM daemon (systemd-oomd, earlyoom) kill a process before the system becomes unresponsive.
  • zram (compressed swap in RAM) on embedded devices without disk swap, as Android and ChromeOS do.
  • Avoid swap entirely on real-time systems and use mlockall() for time-critical processes.

Embedded note: most embedded Linux devices have no swap, so instead of thrashing on swap they thrash on file-backed pages: code pages of running programs are evicted and re-read from flash over and over, which also wears the flash. Watch memory headroom in testing.