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

How much SWAP would you give a CentOS install on a dual Quad Core XEON with 16GB of RAM that is going to be running a php website with a mysql backend - medium/heavy load?

share|improve this question
add comment

6 Answers

up vote 17 down vote accepted

Not more than a gig or two -- you want a little bit of swap available, just in case and because it helps things work a bit better, but if you ever get to the point where you're heavily swapping, the machine is going to be useless -- and in a real catastrophic situation, you're actually better off with a smaller swap space, because then you'll trigger the OOM killer sooner rather than later.

share|improve this answer
7  
And remember that you can always add more swap space later if it is needed by making some space and creating a new volume (if using LVM) or using a file as swap with something like dd if=/dev/zero of=swap.file bs=1024 count=1048576; mkswap swap.file; swapon swap.file –  David Spillett Aug 3 '09 at 17:27
3  
As a side note, you can control the degree of swapping by adjusting the /proc/sys/vm/swappiness value. –  David Aug 4 '09 at 0:40
2  
I'm gonna have to disagree with a few points here. . . First, disk is cheap, you might as well throw a couple of GB at it. Secondly, OOM killer can take down your whole box if you get unlucky. Relying on it in any way or considering it better to trigger the OOM killer is a Very Bad Idea. Thinking that a potentially hosed box is better than a potential slowdown is also a bad idea. The only time that would be reasonable is if you had a very specific setup planned around that situation. –  Christopher Cashell Nov 14 '11 at 21:04
    
side note, its generally preferable to set swap to a partition and not a file to avoid overheads and simplify the boot process. however using a file will work. 4GB is all any system should need, i doubt you will get much swapped anyway with 16gb of RAM –  Silverfire Nov 16 '11 at 2:19
    
Actually according to Andrew Morton, the performance should be about the same between a swapfile and partition lkml.org/lkml/2005/6/29/11 –  Journeyman Geek Nov 18 '11 at 2:41
add comment

I apologize for the very long answer. I've been wanting to put this stuff somewhere accessible for a while!

TLDR; Theoretically speaking, the answer would be "probably no less than the total amount of resident memory all your applications want to use"

I'll try to explain if you stay with me..

A bit about virtual memory

I feel there is a common misconception these days about what the value of swap is and what its purpose is for. It is often considered that swap is meant as a 'reserve bank' for memory when your running low on memory. Yes, this is partly true, but the kernel doesnt want to use your swap as a reserve bank. Additionally the kernel never wants to invoke the disk to get the data you are after!

In application space, theres a number of things the kernel will be keeping in memory.

  • Mapped file data.
  • tmpfs filesystems.
  • Raw application memory allocated at runtime.
  • Application code (such as stuff in the data segment of ELF formats)
  • Privately mmaped file data.

For the purpose of memory management, memory allocation is backed in some form or another by a supporting device.

File backed memory is memory which has come from a file and on a typical O/S makes up the vast majority of memory allocation on the system. It includes files such as shared libraries which have been loaded, files read from disk and stored in the page cache and files mmaped from disk (in fact, the kernel makes no distinction of pages between files in the page cache and files mmapped as its essentially the same thing).

The great thing about this memory from the kernels standpoint is its disposable, that is it should be possible to dump these pages if you need the memory for something else and this is exactly what the page cache does if memory suddenly is a requirement.

Anonymously backed memory is a different matter. Memory from this region is anonymous because, well there is no file on disk which actually contains this data. This is normally made up of the application stack, the heap, anything in tmpfs and mmapped data which is private and has been modified (since it cant sync this stuff back to disk). Since theres just no valid file on the filesystem to write these pages back to if they change, anonymously backed memory is backed by the swap media.

Now, the kernel knows that when memory is scarce, its far far cheaper to ditch file backed memory the anonymously mapped memory, thats because anonymous data has a much higher chance of being "dirty" than file backed data, in fact by default the kernel rates anonymously backed memory as being 80 times more valuable than file backed memory and this is actually what the swappiness modifier does on linux (see this post here if you want to know what exactly the swappiness parameter is altering).

The worst case scenario

The worst case scenario when it comes to a server going out of control and OOMing is based off of the fact that it spends far too much time handling I/O requests than honouring memory allocation requests. There are two conditions which can invoke this criteria.

  • Swapping anonymous memory around all the time to fetch pages in demand, or allocate more pages for an application.
  • Spending too much time fetching data from disk because its not in memory than you give to execute processes wanting CPU time.

The first one, is the commonly thought of problem. That is, since so much of the memory lives inside of swap, one needs to swap anonymous memory out of RAM, put it back into swap, then take something from swap and put it into real RAM. This operation is very expensive, slowing down the machine to the point where it can become an unrecoverable situation (as more stuff is queueing for page demands than what can be served from I/O).

The second one is less considered but just as important. If you allocate almost all your memory to real application data - you wont last long. Nearly every application relies on reading files from the filesystem to operate, this could be because some instructions live in a shared library or because you need to read /etc/resolv.conf for a library call, or any other purpose. Its entirely plausible to halt an operating system - yet have enough memory to fit all your applications, but since your queueing so much I/O requests up nothing has a chance to complete properly.

What the kernel wants to do with your swap

The kernel wants to use your swap to get rid of pages that are wasting memory so it can use that memory for something else.

Basically, in normal operation, the kernel loves to aggressively fill up page cache with data read from disk, this means it wont read the disk for the same data. This is good design and can massively reduce I/O. Now, it might be that you have some application sat in memory that sleeps for 3 days, wakes up, does a bunch of work then sleeps another 3 days.

What the kernel would like to do with this data is swap it out to make space for filesystem activity instead, since you have a much higher chance of actually using these pages more often than the pages you use for your application. Swapping, in this sense might be a 16kb transaction to your swap media which you should hardly feel, but in return you freed up 16kb of memory that could be used to store four files worth of data.

What the kernel doesnt want to use swap for

The kernel definitely doesnt want to use your swap to allocate more anonymous memory by swapping out some other anonymous memory, this is the situation people worry about the most and rightly so.

However, I should point out that if you have allocated so much memory that the kernel has no choice but to do this, this is a configuration problem of the system administrators, not the kernel itself -- its just trying to do the best by the options you've given it!

If you have a huge amount of swap do you increase the chance you use it?

No! If you have 1G of ram and 4G of swap, theres not a 80% chance your data gets swapped! The kernel wants to use the swap only when pages in memory can be better served doing something else!

Is it advantageous to not use swap at all

I would never do this. Swapping allows the O/S to get rid of memory you need to have but is never in use. If you have no swap, your just swallowing up memory you'll never get back, for which you might see a significant performance improvement by allowing say, the page cache to have it instead.

Whats the best swap to have

Theoretically speaking, find out how much resident memory +20% for safeties like re-entrant library calls that have to allocate memory from heap - then set your swap to that amount. This would (theoretically anyway) permit the operating system to swap all anonymous memory out if it had to to make way for something more useful.

If I get the kernel opportunity to swap out everything thats dangerous right?

Remember, the kernel isnt wanting to swap to make way for more anonymous memory allocation here, it will only ever swap out pages not in use to favour something else that will make better use of the space instead.

If your swapping anon memory out only to allocate from more anonymous memory, your doing something wrong and need more RAM or to retune your application stack anyway.

How much RAM would you need

You need to permit enough RAM to run all your applications of course, but you should probably allow an extra 2G of RAM for pagecache to fill up -- maybe more. Page cache makes your computer much much faster and your disks last longer. If your thinking of running a webserver, having even more for pagecache is a good idea due to the sheer amount of static content you can retrieve and reuse from pagecache that would get served out (if your webserver throughput is 5mb/s, you really dont want to be retrieving that 5mb/s content you generating from your disk after all!).

What to do if you really dont trust linux to swap properly

If your really concerned you can allocate more memory than you have:

  • Set your swap amount to be no more than you RAM
  • Set /proc/sys/vm/overcommit_memory to 2
  • Set /proc/sys/vm/overcommit_ratio to be a value which can never actually exceed physical RAM limits. See the kernel documentation to work out what that number would be to you.

Whats the best way to tune my memory for my application

  • Understand what swappiness actually does.
  • Use CGroups to allocate the correct resources per application.
  • Change the overcommit mode mentioned above to make the operating system enforce strict limits.
  • Use cgroups as above to set OOM priorites on applications you really want to keep if your out of memory and application you really want to ditch if your out of memory.

The vendor recommends a different configuration..

Listen to that instead. Some applications are written in such a way that they deliberately invoke the kernel to have their pages active over all others. This is a nasty trick to be honest but it breaks the kernels ability to seamlessly manage memory when it happens. If your vendor is giving you specifics then they likely fall into this category and listen to what they say instead.

In summary

  • Swap is meant to be used as a place to put wasted memory. Not as 'spare memory'
  • Having huge amounts of swap has no effect whatsoever on your chances of using it.
  • The kernel really wants to stop you accessing the disk for your data. This applies just as - if not more equally to page cache as it does to swap space.
  • The kernel will try its best to honour the setting you give it to the apps you run. But dont be surprised if you OOM if you have 500 apache children all taking up 32M of memory. Thats a fault of the configuration you choose, not the memory management.

The kernel usually does a very good job of managing virtual memory properly. Its almost always the case that your application is allocating more memory than you could possibly hope to work with and thats what is causing an OOM.

Swap used to be used as 'spare memory', but its not its primary purpose anymore, so dont think of using it like that. Instead, appreciate that your kernel probably knows best what it wants to use your memory for. Give it space to make those decisions and you'll benefit from an overall performance improvement.

share|improve this answer
add comment

From document "Oracle 10g Server on Red Hat® Enterprise Linux® 5 Deployment Recommendations".

Oracle provides generic recommendations regarding the size of swap in MetaLink Note 169706.1. These recommendations may lead to creation of very large swap space on systems with large amount of memory. The very large swap may cause sever system performance degradation and can be resolved by reducing swap space. Red Hat does not recommend allocating greater than 4GB for swap on Red Hat Enterprise Linux 5.

So for 16GB you should have no more than 4GB of swap.

PS. Also it's worth to note that invoked oom is almost always better than unusable swapped out process using whole I/O bandwidth.

share|improve this answer
add comment

Current CentOS Swap recommendations use the formula:

 M = Amount of RAM in GB, and S = Amount of swap in GB, then

If M < 2
    S = M *2
Else
    S = M + 2

So for 16G, the amount of swap should be 18G.

See http://www.centos.org/docs/5/html/Deployment_Guide-en-US/ch-swapspace.html

share|improve this answer
add comment

From what I recall from linux documentation: If your ram is greater than 2gb then you do swap = (ramSize + 2). If it's less then you do swap = (ramSize*2)

share|improve this answer
add comment

I found this document for you, it should help you to make that decision, here it is: What is Swap Space?, but neither of above answers are correct

share|improve this answer
1  
The document you linked to doesn't agree with itself. Following the stated rule of "for more the 2Gb RAM amount of RAM above 2Gb" would give 1Gb swap for 3Gb RAM (3-2=1) not the 5Gb. –  David Spillett Aug 3 '09 at 17:24
add comment

protected by Iain Nov 16 '11 at 7:55

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?