Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Recently my friend told me that it is a good idea to turn off swap on linux webservers with enough memory. My server has 12 GB and currently uses 4GB (not counting cache and buffers) under peak load.

His argument was that in a normal situation server will never use all of its RAM so the only way it can encounter OutOfMemory situation is due to some bug/ddos/etc. So in case swap is turned off system will run out of memory that will eventually crash the program hogging memory (most likely the web server process) and probably some other processes. In case swap is turned on it will eat both RAM and swap and eventually will result in the same crash, but before that it will offload crucial processes like sshd to swap and start to do a lot of swap operations resulting in major slowdown. This way when under ddos system may go into a completely unusable condition due to huge lags and I probably will not be unable to log in and kill webserver process or deny all incoming traffic (all but ssh).

Is this right? Am I missing something (like the fact that swap partition is very useful in some way even if I have enough RAM)? Should I turn it off?

share|improve this question
4  
So, all the answers boil down to statements that 1. more virtual memory is unconditionally better than less, and 2. there must be some swap enabled because it's good neither of which are well grounded. –  NekojiruSou Jun 13 '12 at 10:22
1  
It is misguided to think that swap is exclusively meant for 'reserve' memory. Swap is meant for a place to put unused memory (so you can use your actual memory more efficiently). You should always have swap. If your out of memory, then your out of memory - period. This is a problem of capacity management - not tendency to swap. I have a rather long response serverfault.com/a/332205/75118 that tries to better explain the virtual memory intentions. –  Matthew Ife May 1 '13 at 8:09

8 Answers 8

up vote 11 down vote accepted

I would say it depends on your use case and the rest of the answers have covered this pretty well. 4G of swap are after all a cheap way to buy some safety. And I feel that this cheapness is what is making people not want to turn it off.

But let me answer with a rhetorical question. If money is not an issue, and you have a choice between two systems - one with 12G of RAM and 4G of swap, and another with 16G of RAM and no swap - which one would you choose? Unfortunately most people would still answer that they'd choose 16G of RAM and still add 4G of swap, which is missing my point.

And on another note, I personally find a swappy system worse than a crashed system. A crashed system would trigger a standby backup server to take over much sooner. And in an active-active (or load balanced setup) a crashed system would be taken out of rotation much sooner. A win for the no-swap system again.

share|improve this answer

It is NOT recommended to turn off swap even if you have enough memory. If your server needs more memory and it did not get it, it will crash. However, this can be prevented (to some extent) when you have a swap area.

Yes, your server performance will degrade when using swap, but at least it will be operational and accessible. Then, you can plan for adding more memory when needed if your server starts using swap.

I found this page talking about swap. Have a look at 3rd section.

Instead of turning off swap, you can control the swapiness.

share|improve this answer
1  
In situation I described it will consume both swap and RAM and also will crash. It is running for a few months and it never used more than 30% memory. So it is very unlikely that it will jump to 100% so quickly during normal operation. So if some process went crazy and consumed 8GB it means that it will consume as much memory as OS can get, that means all of it. –  Poma Jan 22 '12 at 11:42
    
@Poma, there are other benefits of swap as well. Also, you can allow the system to live much longer when you have a swap. This is because swap can be larger than memory given the availability of cheap HD drives. –  Khaled Jan 22 '12 at 11:43

No, it is not a good idea. 'some process went crazy' means you should have proactively already called

ulimit -d

at or before process creation time to set a limit on per process data segment memory -- And maybe a limit to the number of threads

ulimit -T

per process. ulimit is your friend. Please consider reading one of the memory tuning guides before you turn off swap. You can change kernel parms as well to some things to try to handle DOS attacks or bad programs.

Look at it this way: The total memory on your system is RAM + swap. If you have 12GB of swap, you just effectively cut the system VM capacity in half, by disabling swap. Bad idea. This isn't a debate, really, it is simply reading what other people have known from previous bad experiences for years. It is possible your friend needs to do some reading as well.

share|improve this answer

Another good thing that you can do is swap into RAM using zRAM. I think that this is a great idea! For performance, is like not having swap at all, but also prevents the crashes when the system is very loaded!

Look at this:

http://www.webupd8.org/2011/10/increased-performance-in-linux-with.html

My experience: In this machine where I'm writing now, I disabled the swap, cause I have 4Gb RAM (in 2009 it was a lot!). I only experienced a couple of problems, one of them was opening like 127 pictures at the same time by mistake!

BUT.. this is a workstation, and I can afford to reboot if it hangs. In a server I think is better to have swap, and the swap-in-RAM sounds good to me.

share|improve this answer

As others have said, you can effectively stop your server using swap except when absolutely necessary by tinkering with the "swappiness" parameter. This controls how aggressively the kernel will swap out memory pages.

You can see what it's currently set to with:

cat /proc/sys/vm/swappiness

and you can edit it "live" with (as root):

# echo "10" > /proc/sys/vm/swappiness

and to make it persist, add the following to /etc/sysctl.conf:

vm.swappiness=10
share|improve this answer

Not a good idea. You can define 2 swap files with different priority. One smaller that is in use and one larger that will be used in case the first fills.

Also vm.swappiness can help you control how aggressively the disk swapping occurs.

share|improve this answer

As has already been made abundantly clear, this is not a good idea. If nothing else, swap gives you some breathing space for when things aren't quite normal. e.g. On one system I look, which normally has only a few visitors per day, there was a massive traffic spike caused by a page being mentioned in a magazine. This resulted in the web server using swap space for the first time since it was commissioned. Without that swap space things would not have gone very well.

share|improve this answer

If you inset a 0 to vm.swappiness that doesn't necessarily mean that the system will not swap. It is a parameter to determine how aggressive kerne's tendency would be to swap but it doesn't turn off swap.

Again, swapping is not bad but thrashing is. Take a look of the sysstat data and that should give pretty good pointer on it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.