Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
When RAM fills up, the OS starts swapping pages to disk, which is thousands of times slower. Learn about thrashing, memory pressure and real fixes.
Quick answer: When programs need more memory than you have physical RAM, the operating system frees space by moving less-used data out of RAM and onto your disk, a process called swapping or paging out. Disk is thousands of times slower than RAM, so every time a program needs that data back, it has to wait. If this happens constantly, the machine spends more time shuffling data than doing work. That state is called thrashing, and it is what a "frozen" computer with a busy disk light usually is.
Also Read: How CPU Scheduling Works in Your Operating System - How To's
The whole story comes down to a speed gap. Reading a value from RAM takes on the order of 100 nanoseconds. Reading from an SSD takes tens to hundreds of microseconds, and a spinning hard drive takes several milliseconds. The classic latency numbers list makes the scale clear:
| Operation | Rough time | If RAM access took 1 second |
|---|---|---|
| Main memory reference | 100 ns | 1 second |
| SSD random read | around 100,000 ns | about 17 minutes |
| Hard disk seek | around 10,000,000 ns | about 28 hours |
So when data that should be in RAM lives on disk instead, each access can be thousands to a hundred thousand times slower. For why computers have both kinds of storage at all, see why we need both RAM and storage.
Thanks to virtual memory, programs do not use RAM directly. The operating system decides which pages of memory live in RAM at any moment. As free memory shrinks, it works through a list of options, from painless to painful.
Unused RAM is wasted RAM, so operating systems fill spare memory with a page cache: copies of recently read files, kept in case they are needed again. This is why opening an app a second time is faster. When memory gets tight, this cache is the first thing to go. It costs nothing to drop, because the data is still on disk.
This is also why "free memory" looks alarmingly low on a healthy system. Most of that "used" memory is cache that can be released instantly. Look at "available" memory instead.
macOS, Windows and many Linux setups (using zram or zswap) compress idle pages and keep them in RAM. Compressing costs a little CPU but is far faster than using the disk.
Next, the OS writes pages that have not been used recently to a swap file or swap partition and reuses their RAM. When a program touches one of those pages again, a page fault occurs and the page must be read back from disk before the program can continue.
A little swapping is harmless. A background app you have not touched for an hour can live on disk without you noticing.
Trouble starts when the pages you are actively using do not fit in RAM. Then the cycle looks like this:
The system is now busy, the disk is working constantly, CPU usage by your applications is actually low, and nothing responds. This is thrashing.
If memory and swap are both exhausted, something must be stopped. Linux runs the OOM (out of memory) killer, which picks a process and terminates it. Phones are more aggressive: iOS and Android routinely kill background apps to free memory, which is why an app sometimes reloads when you switch back.
On a hard drive, swapping involves moving a mechanical arm for each read. On an SSD, there is no moving part, so swap is much faster and the slowdown is more of a stutter than a freeze. It is still far slower than RAM, and heavy swapping adds write wear to the drive. See why SSDs are faster than HDDs.
free -h for the "available" column, and vmstat 1 to watch the si and so columns (swap in and out). Consistently non-zero values mean active swapping.If the disk is busy, memory is nearly full, and CPU use is low while everything feels stuck, you are thrashing.
Not by itself. Operating systems deliberately use spare RAM for caching. It only becomes a problem when available memory is low and the system is actively swapping.
Only if you were running out. Extra RAM beyond what your workload needs sits unused, apart from a larger file cache.
They are the same idea under different names: swap space on Linux and macOS, the page file on Windows.
Other programs' pages were pushed to disk while memory was tight. They are only read back when touched, so each app stutters the first time you return to it.
A computer does not slow down gradually as RAM fills; it falls off a cliff when the data in active use no longer fits. At that point the operating system substitutes disk for memory, and the huge speed gap between the two does the rest. Watching available memory and swap activity tells you whether you need fewer tabs, a restart, or more RAM.