Hacker News new | past | comments | ask | show | jobs | submit login

Most people think of swap as "emergency memory in case I run out of memory" and while it's true that it can get used in this way, it usually serves a much more critical purpose in your OS's ability to reason about and use memory.

For a good article on why this is true for Linux: https://chrisdown.name/2018/01/02/in-defence-of-swap.html

I believe that most operating systems are going to make use of memory in a similar manner.

With that said, I'll turn off swap on devices that have unreliable storage. (Anything using an SD card)




I've always hated one bit of that article. It doesn't address the "low" and "no" memory contention cases separately, even though they're quite different and it's possible to get a system to stay in the "no memory contention" case all the time (unless there's a memory leak, but swap just fills up then and provides no benefit).

> Under no/low memory contention

> With swap: We can choose to swap out rarely-used anonymous memory that may only be used during a small part of the process lifecycle, allowing us to use this memory to improve cache hit rate, or do other optimisations. > Without swap: We cannot swap out rarely-used anonymous memory, as it's locked in memory. While this may not immediately present as a problem, on some workloads this may represent a non-trivial drop in performance due to stale, anonymous pages taking space away from more important use.

This is under the low/no memory contention. For the low memory contention case, this can make sense, but for the no contention case it's nonsense. There's no more important use, all uses are fulfilled and there's still free memory (that's what "no contention" means)!

So clearly that very page has presented a case where swap is useless: when you have enough RAM to ensure there's never contention.

Swap also only extends the amount of memory by the size of the swap partition. If you've got 64GiB of memory and an 8GiB swap partition, you could just as well have 96 or 128GiB of memory and reserve an 8GiB zram for swap.

Indeed, Fedora changed to use zram instead of swap-on-disk by default in Fedora 33[1].

Swap does allow some fancier optimizations to memory layout, but again swap-on-zram is better for this than on a disk if you've got enough RAM.

The big benefit of swap is on laptops where hibernation may actually be desirable (assuming encrypted swap & disk) and RAM is harder to come by. A laptop with 96GiB+ of RAM is a LOT more of an expense than a desktop with the same.

One huge disadvantage of swap-on-disk that article neglects to mention is that sensitive data from RAM can be written to persistent storage (swap) and henceforth leaked more easily (assuming unencrypted swap). Swap must be encrypted if it's disk-backed.

[1] https://fedoraproject.org/wiki/Changes/SwapOnZRAM




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: