How to: Configure Swappiness in Ubuntu

How to: Configure Swappiness in Ubuntu

In one of my previous articles on how to setup virtual memory / swap memory in Ubuntu I covered how to install a swap space for a Windows Azure VM. I deployed swap mostly as a precaution against running out of memory which could result on applications unable to work properly and the site could go down. To avoid that you deploy swap which most likely decrease the speed of your services but at least they won’t crash. It effectively buys you time to resolve your memory issues at the expense of using the HD resulting in added latencies. So thus far setting up your swap memory seems genius, but beware, if your swappiness is not set up properly you will face unecessary latencies. In my case, I had still over a gigabyte of memory available but I was already swapping a lot. There is no point in swapping when there is plenty of RAM left; You are just moving information in and out of memory that could easily fit all in memory. Swappiness then controls how aggressively should the operating system use the swap space.

What is Swappiness?

The swappiness parameter in Ubuntu controls the tendency of the OS’ kernel to move memory (information) used by processes out of physical memory and onto the swap disk. As mentioned above using a hard drive is supposed to be much slower than using RAM by nature. This results in applications taking longer to respond, particularly those who are being swapped to disk (the others suffer indirectly because of the added IO operations but at least they are present in RAM already)

There are some important things to know about how to use this parameter:

  • Accepted Swappiness values range from 0 to 100 inclusively.
  • A value of 0 indicates the kernel to avoid swapping at all costs.
  • A value of 100 indicates it should aggressively swap information on memory.

So thus far you must be thinking after reading my thoughts on swappiness that 0 is the way to go: be careful with that choice. The correct answer depends on many things including your applications, the use and capacity of the system. For example, assume you use a value of 0 and memory is almost full. If you try to launch a large application there will be a delay until the system is able to free enough memory by swapping processes before you can use your application. Take the other extreme of 100: You should at all times have plenty of RAM to launch large processes but your overall response rate of your machine will take a performance hit.

Ubuntu has determined by default that a value of 60 is ideal for most users. It allows for memory to be available to launch new applications while is not that aggressive as to cause a huge performance hit. But as I mentioned the best value for your machine depends on the use you give the system. For example, I am running a server that eventually launches processes to service incoming requests but their size is relatively small and for the most part I always have plenty of RAM to handle all the services needed. In my case I like using a swappiness value of 10.  You should think about your general RAM usage patterns and feel free to experiment. I expect boundary values (0 and 100) are too extreme but you can see what is the difference between 25 and 75 for example.

How do I configure Swappiness?

This is rather easy. There is a file (/proc/sys/vm/swappiness) that it is used during operation that indicates the current swappiness the system is running under. Changing this value would result on the swappiness of the system being changed for the duration of the kernel (i.e. until you reboot / shutdown & turn back on).

If you want to see the value I recommend using cat over nano to avoid accidentally editing the file:

$ cat /proc/sys/vm/swappiness
60

You’ll probably see the default value of 60 unless it was modified already. As I mentioned you could modify the file, swapoff and back on to have the change take place only during your current session. If you modify the value via the sysctl command (I recommend not editing the do not edit the /etc/sysctl.conf file directly to avoid accidentally breaking something else). Said change would be take place only after you reboot the machine. Suppose you want to use a swappiness level of 15:

$ sysctl vm.swappiness=15

If you want you can execute swapoff -a and swapon -a to get your new swappiness value applied to the system (instead of having to reboot this could be an attractive option).

Don’t forget to check your memory usage with free -m to see the swap size and usage.

What swappiness value do you recommend?

This is my personal opinion and you should monitor your swap usage and performance to fine tune it. I believe this is a good starting point but use your own criteria, experiment and decide what is best for you.

Recommended Swappiness
Memory generally available Relative size of new processes Recommended Swappiness
A lot Small < 10
A lot Medium 10 < x < 20
A lot Large 15 < x < 30
Medium Small 10 < 15
Medium Medium 15 < x < 25
Medium Large 25 < x < 45
Low Small 35 < x < 60
Low Medium 50 < x < 70
Low Large 65 < x < 90

It all becomes relative but you get the general idea: The more memory you generally have free or available the lower the swappiness you can use. Also, the larger the new processes you launch the larger the swappiness you’ll need so they can properly fit in memory if need be. Consider the following scenario: You have a machine with 16gb of RAM and you generally only use 2gb. You have never used more than 13gb and don’t expect to. So even if new processes use a lot of RAM you never really run out of memory so a value at 10 or below would be recommended (even 0).

If you want feel free to provide via the comments your system’s specs, memory usage patterns and I can help you pick a starting value to play with!

Enhanced by Zemanta

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.