Linux – Specific Functions of Swap That RAM Can’t Perform

linuxswap

What specifically can Linux do when it has swap that it can't without swap?

For this question I want to focus on the difference between for example, a Linux PC with 32 GB RAM and no swap vs. a near identical Linux PC with 16 GB RAM with 16 GB swap. Note I am not interested in the "yes, but you could see X improvement if you add swap to the 32 GB PC". That's off-topic for this question.


I first encountered the opinion that adding swap can be better than adding RAM in comments to an earlier problem.

I have of course read through this: Do I need swap space if I have more than enough amount of RAM? and…

  • Answers are mostly focussed on adding swap, for example discussing disk caching where adding RAM would of course also extend the disk cache.
  • There is some mention of defragmentation only being possible with swap, but I can't find evidence to back this up.
  • I see some reference to MAP_NORESERVE for mmap, but this seems a very specific and obscure risk only associated with OOM situations and possibly only private mmap.

Swap is often seen as a cheap way to extend memory or improve performance. But when mass producing embedded Linux devices this is turned on its head…

… In that case swap will wear flash memory, causing it to fail years before the end of warranty. Where doubling the RAM is a couple of extra dollars on the device.

Note that's eMMC flash NOT an SSD!. Typically eMMC flash does not have wearleveling technology meaning it wears MUCH faster than SSDs


There does seem to be a lot of hotly contested opinion on this matter. I am really looking for dry facts on capabilities, not "should you / shouldn't you" opinions.

What can be done with swap which would not also be done by adding RAM?

Best Answer

Hibernation (or suspend to disk). Real hibernation powers off the system completely, so contents of RAM are lost, and you have to save the state to some persistent storage. AKA Swap. Unlike Windows with hiberfil.sys and pagefile.sys, Linux uses swap space for both over-committed memory and hibernation.

On the other hand, hibernation seems a bit finicky to get to work well on Linux. Whether you "can" actually hibernate is a different thing. ¯\_(ツ)_/¯

Related Question