3 ms·
Swap implementations have gotten a lot more sophisticated and complicated over time. But fundamentally it's an undesirable thing to be dependent on it as it is
by jillesvangurp 5y ago
Swap implementations have gotten a lot more sophisticated and complicated over time. But fundamentally it's an undesirable thing to be dependent on it as it is just a lot slower than normal memory.
Part of the problem is that there is a lot of magical thinking rooted in decades of half truths about swapping on various operating systems with completely different implementations. Most of those per-conceptions if they ever were true at all, are now obsolete.
For example, Windows XP used to have swapping. It commonly sucked the life out of any PC it was on. For no good reason whatsoever. Symptoms: the system performance would degrade over time and people would routinely reboot to fix that. Even with plenty of memory on a system, it would spend lots of time swapping things in and out of memory. Very infuriating if you just spent a lot of money on RAM. It was optimized for people with cheap hardware. Computers with enough memory were an afterthought. So it pretended you were running out of memory even when you weren't.
Easy solution: disable swap completely; especially on machines with more memory than the total addressable memory per process on 32 bit windows XP (2 GB). I remember having a particularly slow windows laptop with 4GB of RAM. My life improved massively after I disabled swap on that thing. Alt tab was instantaneous; always. I could get individual processes to crash with an out of memory error but only if I deliberately opened way more processes than fit in memory (so, don't do that). Exactly the kind of thing that would make the laptop completely unusable with swap on in any case. I actually preferred the quick crash. Trying to close a non responsive application to free up some memory while the system is 100% engaged with grinding your disk to fine dust is not fun.
Linux is way more complicated and it depends on the use case. A swapping server is a dead server. So, you should be conservative with any kind of swap there. Basically, if you have enough memory and no memory leaks there should be no need for it to degrade response times (aka swap), ever. If you do have memory leaks, swapping is actively harmful because it degrades response times. Better reboot the server. Or rely on the docker oom killer. Or assign more memory. Spin up more servers. Etc. And of course you monitor memory usage on servers to preempt any kind of issues. Swapping is the punishment that happens on a poorly, configured un-monitored server. You should have no need for it.
For a laptop with not enough memory because the buyer tried to save some cash and bought something with a pitiful amount of memory, definitely have swap space available. Performance will suck but at least it will be able to run some software. And Linux isn't as bad at this as Windows XP used to be. People actually care.
Otherwise, you probably want it to be able to hibernate and you need swap space for that. But perhaps with a very low swappiness value so that it won't actually swap. Or in case you don't need hibernate, turn it off completely and save some disk space.
- zozbot234 5y ago> For example, Windows XP used to have swapping. It commonly sucked the life out of any PC it was on. For no good reason whatsoever. Win XP-era machines had tiny amounts of RAM, so it makes total sense that swap would be heavily relied on.
- jillesvangurp 5y agoThe windows XP era was quite long. I used it between 2001 and 2008. The laptop I mentioned, had more memory than the 32 bit version could technically allocate for a single process and most processes used far less than the maximum. So, I could run one big process like that and have all the usual stuff in the background without much issues. The 64 bit version was released later and of course allowed for more memory. They never really addressed the swapping issues. So it continued to make sense to upgrade the memory and switch off swapping as opposed to replacing the entire laptop because it was "slow". Most instance of people complaining about their laptop performance basically were running into this and never realized the fix was a 100$ upgrade of their memory + simple configuration change. A lot of those laptops are still fine for running Linux today.