SwapOff

To swap, or not to swap still seems to be a common question. Modern computers often provide massive amounts of memory, which is great because memory is fast. Swap, being disk based, is slow. The benefit of swap is that it provides more memory resources for a system with high utilization or insufficient capacity.

Some of the confusion surrounding swap may stem from a bug in the mount manpage where swap isn't particularly well documented and the filesystem specific mount options for swap spaces simply are not included (though these are mentioned in the swapon manpage). But before we get into options let's look at when we need swap.

The old rule of thumb, outlined in this page says:

  • for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications;
  • for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary;
  • for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB.

The problem with this recommendation is that modern desktops can simply provide more memory than they need. Consider my desktop, which I use quite heavily. It offers 16G of memory and while it commonly uses 8G, it rarely uses much more than that. Therefore I don't use a swap partition. Everything stays in memory and runs very fast. I used to have a swap partition, but the system did not make good use of it; the system would store old information in swap, which caused lag whenever it went to retrieve it. Particularly, delays would be apparent when switching between applications or starting new applications.

New rule of thumb:
If you have ample RAM, simply don't use swap. Your system will run faster.

But what about legacy systems?
Let's look at swap files. If your swap partition isn't sufficient and you can't fix your disk partitioning, just add a swap file, right? Well consider that the kernel will automatically stripe swap partitions. So if your swap file is on the same disk as your swap partition, the result will be the R/W heads will excessively jump between the file and the partition, which will degrade overall performance and can contribute to premature disk failure.

How can we fix this? Pay attention to swap priority when building fstab. Lower swap priorities will not be used until higher priorities are exhausted and swap spaces with the same priority will be striped. Priority values range from 0 to 32767 and bigger number indicates higher priority.

In this example, swap partitions on sda and sdb will be striped and the swapfile will not be used until the swap partitions are over capacity.

/dev/sda3 none swap defaults,pri=100 0 0
/dev/sdb3 none swap defaults,pri=100 0 0
/swapfile none swap defaults,pri=10  0 0

For more information, arch has some great swap documentation.

Here's the technical explanation for how swap space is allocated according to priority, taken from man 2 swapon
Each swap area has a priority, either high or low. The default priority is low. Within the low-priority areas, newer areas are even lower priority than older areas.

All priorities set with swapflags are high-priority, higher than default. They may have any nonnegative value chosen by the caller. Higher numbers mean higher priority.

Swap pages are allocated from areas in priority order, highest priority first. For areas with different priorities, a higher-priority area is exhausted before using a lower-priority area. If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.