24

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?

Journeyman Geek
  • 6,969
  • 3
  • 31
  • 49
Poma
  • 1,289
  • 6
  • 22
  • 35
  • 6
    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
  • 2
    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 http://serverfault.com/a/332205/75118 that tries to better explain the virtual memory intentions. – Matthew Ife May 01 '13 at 08:09

8 Answers8

28

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.

chutz
  • 7,569
  • 1
  • 28
  • 57
15

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.

Khaled
  • 35,688
  • 8
  • 69
  • 98
  • 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
6

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.

jim mcnamara
  • 429
  • 3
  • 8
5

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
2

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.

CyberPlant
  • 21
  • 1
1

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.

John Gardeniers
  • 27,262
  • 12
  • 53
  • 108
0

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.

Dragos
  • 349
  • 1
  • 2
  • 11
0

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.

Soham Chakraborty
  • 3,534
  • 16
  • 24