Yes.
You should most definitely always have swap enabled, except if there is a very compelling, forbidding reason (like, no disk at all, or only network disk present). Should you have a swap on the order of the often recommended ridiculous sizes (such as, twice the amount of RAM)? Well, no.
The reason is that swap is not only useful when your applications consume more memory than there is physical RAM (actually, in that case, swap is not very useful at all because it seriously impacts performance). The main incentive for swap nowadays is not to magically turn 16GiB of RAM into 32 GiB, but to make more efficient use of the installed, available RAM.
On a modern computer, RAM does not go unused. Unused RAM is something that you could just as well not have bought and saved the money instead. Therefore, anything you load or anything that is otherwise memory-mapped, anything that could possibly be reused by anyone any time later (limited by security constraints) is being cached. Very soon after the machine has booted, all physical RAM will have been used for something.
Whenever you ask for a new memory page from the operating system, the memory manager has to make an educated decision:
- Purge a page from the buffer cache
- Purge a page from a mapping (effectively the same as #1, on most systems)
- Move a page that has not been accessed for a long time -- preferably never -- to swap (this could in fact even happen proactively, not necessarily at the very last moment)
- Kill your process, or kill a random process (OOM)
- Kernel panic
Options #4 and #5 are very undesirable and will only happen if the operating system has absolutely no other choice. Options #1 and #2 mean that you throw something away that you will possibly be needing soon again. This negatively impacts performance.
Option #3 means you move something that you (probably) don't need any time soon onto slow storage. That's fine because now something that you do need can use the fast RAM.
By removing option #3, you have effectively limited the operating system to doing either #1 or #2. Reloading a page from disk is the same as reloading it from swap, except having to reload from swap is usually less likely (due to making proper paging decisions).
In other words, by disabling swap you gain nothing, but you limit the operation system's number of useful options in dealing with a memory request. Which might not be, but very possibly may be a disadvantage (and will never be an advantage).
[EDIT]
The careful reader of the mmap
manpage, specifically the description of MAP_NORESERVE
, will notice another good reason why swap is somewhat of a necessity even on a system with "enough" physical memory:
"When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available."
-- Wait a moment, what does that mean?
If you map a file, you can access the file's contents directly as if the file was somehow, by magic, in your program's address space. For read-only access, the operating system needs in principle no more than a single page of physical memory which it can repopulate with different data every time you access a different virtual page (for efficiency reasons, that's of course not what is done, but in principle you could access terabytes worth of data with a single page of physical memory). Now what if you also write to a file mapping? In this case, the operating system must have a physical page -- or swap space -- ready for every page written to. There's no other way to keep the data around until the dirty pages writeback process has done its work (which can be several seconds). For this reason, the OS reserves (but doesn't necessarily ever commit) swap space, so in case you are writing to a mapping while there happens to be no physical page unused (that's a quite possible, and normal condition), you're guaranteed that it will still work.
Now what if there is no swap? It means that no swap can be reserved (duh!), and this means that as soon as there are no free physical pages left, and you're writing to a page, you are getting a pleasant surprise in the form of your process receiving a segmentation fault, and probably being killed.
[/EDIT]
However, the traditional recommendation of making swap twice the size of RAM is nonsensical. Although disk space is cheap, it does not make sense to assign that much swap. Wasting something that is cheap is still wasteful, and you absolutely don't want to be continually swapping in and out working sets several hundreds of megabytes (or larger) in size.
There is no single "correct" swap size (there are as many "correct" sizes as there are users and opinions). I usually assign a fixed 512MiB, regardless of RAM size, which works very well for me. The reasoning behind that is that 512MiB is something that you can always afford nowadays, even on a small disk. On the other hand, adding several gigabytes of swap is none better. You are not going to use them, except if something is going seriously wrong.
Even on a SSD, swap is orders of magnitude slower than RAM (due to bus bandwidth and latency), and while it is very acceptable to move something to swap that probably won't be needed again (i.e. you most likely won't be swapping it in again, so your pool of available pages is effectively enlarged for free), if you really need considerable amounts of swap (that is, you have an application that uses e.g. a 50GiB dataset), you're pretty much lost.
Once your computer starts swapping in and out gigabytes worth of pages, everything goes to a crawl. So, for most people (including me) this is not an option, and having that much swap therefore makes no sense.
/tmp
) after 3 days uptime. – mikeserv Mar 16 '15 at 04:58bcache
for use as their swap file. It can be helpful in those cases. For a baremetal system it is better, in my opinion, to handle load according to hardware specs and tuning config toward best performance as much as can be than it is to rely on swap partitions. But if special cases require it, then loop-mounting a swap file is better (and more easily managed) than partitioning for it. – mikeserv Mar 16 '15 at 21:21ulimit
. Please don't try to generalize such a complicated topic. The fact isswap
is probably nothing more than nuisance if you rein in memory over-commitment, allocation, and out-of-memory killing to your specs. – mikeserv Mar 20 '15 at 12:43fork()
and CoW implications) config for most linux systems to the best of my knowledge. It is not in my experience, however, an optimal config - I've found that explicit caching to tmpfs for processes that need it and killing ancillary others is better overall than overcommitting defacto. – mikeserv Mar 20 '15 at 13:32swappiness=[01]
or something along those lines, the performance will only be positively affected in low memory situations - the opposite is true for all other cases, if to a far (approaching infinitely) lesser extent because all memory is always in RAM.kswapd
is best left sleeping as much as possible - and whenswappiness=>1
the kernel will swap old pages even when memory allows for it to remain. This, though, also depends on stuff like cache pressure and which is most important in your application. – mikeserv Mar 20 '15 at 13:50swappiness
is60
. It has 64GB of swap, of which it is using absolutely nothing. – kasperd Mar 20 '15 at 15:33swap
basically serves no purpose at all if you just dovm.overcommit=2
. – mikeserv Mar 20 '15 at 15:423.2.0-4-amd64
. 32GB of RAM, 28GB used of which 22GB is cache. That's a server which was chosen to have room to grow, and at the time being 32GB of RAM does qualify as more than enough. – kasperd Mar 20 '15 at 16:12