There is a lot of confusion among Linux users with regard to swap space. How much is really needed, and is there...
any way to optimize it?
By default, your Linux distribution will assign some swap space to your computer. Some users think they need twice as much swap as there is RAM in the computer, but this is not the case. Linux only uses swap space if it’s really needed; therefore, in many cases, about 1 GB of swap space is enough, even for some busy server scenarios.
There is a very good reason for these low-swap requirements. Linux only starts using swap space when it is completely out of RAM. When that happens, you'll notice you're out of RAM immediately. Processes that ran fine up to that moment will suddenly become a lot slower. That is because swap space is RAM that is emulated on your computer's hard disk, and a computer hard disk is about a thousand times slower than RAM.
Where other operating systems have the habit of starting to swap at the moment you boot your computer, Linux knows that swap space doesn't do any good for your computer’s performance, so it normally only starts swapping if the computer is almost out of RAM. What “almost” means in this case can be adjusted by using the /proc/sys/vm/swappiness file. In this file, you specify a value that indicates your computer’s willingness to swap. The higher the value in this file, the sooner it swaps. So if you think that you could benefit if your computer starts swapping sooner, change the default value of 60 to something higher (100 is the maximum value). As this is an interface file in the proc file system, you cannot write to the file directly but have to echo the new value into it. For instance: echo 80 > /proc/sys/vm/swappiness would do the job.
If you want to make the new value available after a restart, make sure the sysctl service is started and include the value in the /etc/sysctl.conf file.
In most cases it works fine if you assign a minimal amount of swap space just to make sure your server continues functioning the moment you are out of RAM until you're able to add more. For these cases, 1 or 2 GB is enough.
Swap space usage exceptions
There are some exceptions to swap usage. The first is in laptop computers. As a laptop user, you'd like to see support for hibernation. In order to hibernate the computer, it needs to write the data currently in RAM to swap space. So for hibernation support, you need at least as much swap as you have RAM in the computer. This is not an issue on servers.
One issue on servers is applications that have their own approach to swap space. Two well-known examples are SAP and Oracle. These applications typically need 1.5 times the amount of swap space as there is RAM in your computer. So where an average file server with 16 GB of RAM does fine with just 2 GB of swap, your Oracle server that has 16 GB of RAM will by default need 24 GB of swap. For these kinds of applications, make sure that you follow the recommendations of the application.
If you do need a lot of swap on your computer, it might be interesting to know that you can do load balancing of swap space as well. That means it can make sense to put the swap on different storage devices. The Linux kernel will automatically do load balancing between these storage devices, which leads to better swap performance. And if one of these storage devices is significantly faster than the other devices, you can set swap priorities while mounting it from /etc/fstab. To do this, use the prio=nn option while mounting it; the higher the number you add as the priority argument, the higher the priority for this device.
On modern servers that have lots of RAM, people tend to forget about swap space. However, for some applications, swap is still important for computer performance, and now you’re aware of when it makes sense to add more than the default assignment of swap space.
About the author:
Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. Vugt is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.