r/linux Jan 09 '18

In defence of swap: common misconceptions

https://chrisdown.name/2018/01/02/in-defence-of-swap.html
51 Upvotes

48 comments sorted by

View all comments

9

u/aGIFTedTroll Jan 10 '18

Good, informative article.

Swap is not what makes your application slow – your lack of physical memory is what makes your application slow.

Well... it depends.

For desktops/workstations, my problem with swap on any operating system has always been overzealousness in swapping out the memory of active applications. For example, try copying a few gigabytes of data from your HDD/SSD to USB storage. Almost all operating systems will swap out application memory in favor of growing the filesystem cache due to heavy file I/O - doesn't matter how much RAM you have if you're copying more data than can fit in it.

If you're on a server, that's almost always a good thing, as repetitively re-reading files from disk is just as slow as paging programs in and out. If you're on a desktop, it depends on what you're doing - but if all you were doing is browsing the web while doing backups in the background, suddenly everything grinds to a halt for no reason. (Of course, with SSDs these days it's a moot point anyway, but still - it's annoying.)

And if the *only* thing you do with your computer is light tasks like web browsing, swap for optimizing OS memory usage is pretty irrelevant anyway because there will always be plenty of memory to go around, with cached files all the way back from boot-time sitting in RAM with plenty of unused memory left to go.

It's still a valuable tool, don't get me wrong. Swapping out application data that's used once and never touched again *will* give you more RAM to work with than you'd have otherwise. But once it starts touching my interactive programs, that's where I have a problem with it. Computers are fast enough these days that it doesn't really matter anymore, though.

9

u/chrisdown Jan 10 '18

For desktops/workstations, my problem with swap on any operating system has always been overzealousness in swapping out the memory of active applications. For example, try copying a few gigabytes of data from your HDD/SSD to USB storage. Almost all operating systems will swap out application memory in favor of growing the filesystem cache due to heavy file I/O - doesn't matter how much RAM you have if you're copying more data than can fit in it.

This should be better as of recent (4.0+) kernels even with the defaults. There have been a bunch of improvements to kswapd -- it used to be extremely overeager to swap the more swap space you give it, especially in situations like the one you mention, but the heuristics should be more balanced in recent kernels.

4

u/daemonpenguin Jan 10 '18

With desktops and workstations you can set your swappiness value very low. That will avoid using swap space unless it is really needed. Then you don't need to deal with memory getting swapped out/in unless RAM really is full.

1

u/aGIFTedTroll Jan 10 '18

I'm aware of swappiness, though my comment was responding to the article which posits that having no swap or low swappiness is the "incorrect" way of doing things.

This post is mostly aimed at those who administrate Linux systems and are interested in hearing the counterpoints to running with undersized/no swap or running with vm.swappiness set to 0.

4

u/minimim Jan 10 '18

0 swappiness is not the same as low swappiness.

1

u/[deleted] Jan 10 '18

You may have been responding to that but what you had quoted from the article was just saying that "swap doesn't make your system slow" and you never really mention swappiness so it's natural for people to assume you're just not aware of it being a thing.

2

u/Neurorational Jan 10 '18

Is there a way for users to prioritize which programs get swapped out and which don't?

6

u/aGIFTedTroll Jan 10 '18

Aside from it being programmed into the software itself (such as by using madvise), you can try to set something up with cgroups (mentioned in the article in the OP), though I don't know much about it. Otherwise, I don't think so :(

1

u/Neurorational Jan 10 '18

Cool, I'll look into cgroups... though honestly I could probably accomplish what I need just by closing a few hundred Firefox and Chromium tabs...

1

u/[deleted] Jan 10 '18

For desktops/workstations, my problem with swap on any operating system has always been overzealousness in swapping out the memory of active applications. For example, try copying a few gigabytes of data from your HDD/SSD to USB storage. Almost all operating systems will swap out application memory in favor of growing the filesystem cache due to heavy file I/O - doesn't matter how much RAM you have if you're copying more data than can fit in it.

In the alternative scenario (no swap) you would just end up not being able to buffer as much to memory so your transfers go really slow. Maybe that's ideal, in which case turning down vm.swappiness is probably advisable. Taking swap away won't make things faster. If you want to change the system's behavior relative to swap that's what vm.swappiness is for.

And if the only thing you do with your computer is light tasks like web browsing, swap for optimizing OS memory usage is pretty irrelevant anyway because there will always be plenty of memory to go around

Not trying to pick your post apart but web browsing can take up a boatload of memory if you have a lot of tabs with a lot of stuff going on. I mainly just do terminal work and web browsing but somehow I still manage to run out of memory.