Beginner article: Do we really need swap on modern systems?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Beginner article: Do we really need swap on modern systems?
Quote:
Do we really need swap on modern systems?
This question is asked here and there at LQ. A RedHat employee wrote up an excellent article/primer explaining why you need swap, what happens if you don't have swap, and how much swap you are likely to need.
It seems like an alright rule of thumb, but as with most things, it really depends on the application. Personally, I find swap pretty pointless.
Under normal circumstances (no OOM condition), old pages can get moved to swap to open up ram for disk caching. That seems fine on the surface, but in reality it means that whenever you leave an application running but don't interface with it for a while (eg: leave your email program open on another workspace), it gets moved to swap and takes an eternity to start working again when you want it. I find that beyond irritating. If I don't want rapid access to a program anymore, I'll close it, I don't want the system moving it to swap to make room for more disk cache, because the user experience is destroyed as soon as I try to use that program again. You can use vm.swappiness to prevent that from happening, but then swap never gets used and just sits there doing nothing.
Which brings us to the extraordinary circumstances, running beyond your RAM limits. Swap gives you a buffer zone so if you run past the end of RAM, the data has a place to go without triggering the OOM killer, giving you time to figure out what's happening and fix it. As before, it sounds great on the surface, but in reality I have found that in every-single-case where a system has started running into swap, the system bogs down to the point that it's 100% unusable. It's busy swapping its data and can barely even keep up with mouse movements, much less actually clicking on anything, and you can forget SSHing into a system in this condition. This is usually caused by a runaway process, and will persist until swap is gone too and the OOM killer gets run anyway (or you don't run out, the system finally stabilizes after 20 minutes, you're able to log in, and YOU kill the offending process...same result). So since you can't actually do anything about the problem, all that ends up happening is the system basically locks up for a few minutes before the runaway process gets killed anyway. Why not just run the OOM killer when you first run out of RAM and bypass the 15+ minute lockup stage of the process?
Maybe it's just me, but I've always found swap to be more hassle than it's worth. I still use it, because something in the back of my head starts yelling at me whenever I try to setup a system without, but every time it actually ends up getting used, I'd rather the OOM killer just get called instead. It's going to happen regardless, might as well get it over with.
Last edited by suicidaleggroll; 02-23-2017 at 04:49 PM.
Whereas, I always want to give the operating system an "out."
Obviously, it depends on your situation, but I always like to give Linux the option to "swap out" things, either because they're inactive and taking up otherwise-valuable memory real-estate, or to make sure that it doesn't have to invoke "the dreaded 'OOM [== Out Of Memory] Killer™'" on anything that might actually be important.
I allocate swap space equal to at least twice the size of RAM, and then "fuhgeddaboudit." In normal operations, I find that swap is never used.
Last edited by sundialsvcs; 02-23-2017 at 05:41 PM.
I use swap, because I find Firefox and Chrome will usually eat lots of RAM if left open for a while. I've also found the slower I/O of swap has helped when I've had a script/process run away on me.
It seems like an alright rule of thumb, but as with most things, it really depends on the application. Personally, I find swap pretty pointless.
Under normal circumstances (no OOM condition), old pages can get moved to swap to open up ram for disk caching. That seems fine on the surface, but in reality it means that whenever you leave an application running but don't interface with it for a while (eg: leave your email program open on another workspace), it gets moved to swap and takes an eternity to start working again when you want it. I find that beyond irritating. If I don't want rapid access to a program anymore, I'll close it, I don't want the system moving it to swap to make room for more disk cache, because the user experience is destroyed as soon as I try to use that program again. You can use vm.swappiness to prevent that from happening, but then swap never gets used and just sits there doing nothing.
Which brings us to the extraordinary circumstances, running beyond your RAM limits. Swap gives you a buffer zone so if you run past the end of RAM, the data has a place to go without triggering the OOM killer, giving you time to figure out what's happening and fix it. As before, it sounds great on the surface, but in reality I have found that in every-single-case where a system has started running into swap, the system bogs down to the point that it's 100% unusable. It's busy swapping its data and can barely even keep up with mouse movements, much less actually clicking on anything, and you can forget SSHing into a system in this condition. This is usually caused by a runaway process, and will persist until swap is gone too and the OOM killer gets run anyway (or you don't run out, the system finally stabilizes after 20 minutes, you're able to log in, and YOU kill the offending process...same result). So since you can't actually do anything about the problem, all that ends up happening is the system basically locks up for a few minutes before the runaway process gets killed anyway. Why not just run the OOM killer when you first run out of RAM and bypass the 15+ minute lockup stage of the process?
Maybe it's just me, but I've always found swap to be more hassle than it's worth. I still use it, because something in the back of my head starts yelling at me whenever I try to setup a system without, but every time it actually ends up getting used, I'd rather the OOM killer just get called instead. It's going to happen regardless, might as well get it over with.
I agree, at least partially.
Currently I find it especially annoying when running database ops (mariadb, cassandra, kudu, whatever) for days or weeks => at a certain point the system will start swapping to disk stuff and when I finally want to interact with it I'll have to wait for minutes until I see feedback from the console or the programs.
Personally, I question overall the "size" of the swap - does it make sense to have a swap of 1GB? 10GB? 100GB? Does it really have something to do with the available RAM? Shouldn't it have more something to do with the speed of the device on which the swap is hosted?
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
I'm confused now. I thought that the article suggested that swap may not be needed but could be helpful in stopping memory leaks should they occur?
Personally, I have swap on my laptop because I may decide to "hibernate" and I have a swap file twice the size of the memory on my desktop "just in case" "because I can". If I had a system which actually used swap I'd change the WM or just go with a terminal -- a system using swap is a system that's declared itself dead.
I didn't get much out of it. Since it is a RH article it would mostly direct it's comments toward the support of that product.
" supported by Red Hat - just be sure the behaviour of such a system under memory pressure is what you want. In most environments, a bit of swap makes sense"
My problem here. "design your applications to regularly use swap," No where do I have data on any program easily that tells me if the creator(s) assumed swap.
At one time ram was terribly expensive. Linux users had to make do with very minimal specs. Creating swap was a way to overcome this. Ram still is a limit but hard drive space and now ssd space is really cheap. Who's going to miss a few gig's of space?
Yea, I agree with jefro, not much actual meat in the article for me.
I agree also with creating swap just to give the operating system an "out" should it be needed. It consumes no dynamic resources and very little disk space.
I always create a swap partition and then don't think much about it unless it shows up as being used in my normal monitoring... rarely.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.