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?
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.
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.
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.
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:
Whats the best way to tune my memory for my application
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.
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.
From document "Oracle 10g Server on Red Hat® Enterprise Linux® 5 Deployment Recommendations".
So for 16GB you should have no more than 4GB of swap.
PS. Also it's worth to note that invoked
Current CentOS Swap recommendations use the formula:
So for 16G, the amount of swap should be 18G.
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)
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