You may have experienced system lags, heavy swap usage, or low memory issues in your Linux system. A usual advice is to “decrease the swappiness value”, reduce the swap usage. but what is swappiness? And is it really good to tweak it?
Note: we won’t go into detail about the swap partition. You can read all about it in our article on what you need to know about Swap partition on Linux.
There is a value called swappiness in UNIX-like systems that determines how pages in memory will be handled. Its default value on most systems is “60,” but it can be set to anything between 0 and 100. If memory runs low, the kernel will either evict some file caches to have more free RAM for processes, or it will swap some process pages from RAM to disk.
A default of 60 means swapping will be used less, and I/O caches will more likely be freed for reallocating RAM. Evicting caches is considered “cheaper” (less resource intensive), while swapping out pages includes disk reads and writes, making them more “expensive.” At a value of ’60,’ swap usage will be slightly lower than cache reuse. If the value of swappiness is increased to 100, swapping and file cache eviction will be used with equal weight. This means more swapping and faster I/O than default. Lower values like “10” mean that swapping will be used much less, and I/O caches can be much sooner evicted in favor of processes. This might increase interactivity but could also hurt I/O speed.
You will often see the advice on websites and forums to reduce swappiness to around “10.” This is supposed to speed things up by using less swap and keeping more processes in physical RAM. This logic might be somewhat too straightforward and might not be a “one size fits all solution.”
Decreasing the value of swappiness might be good in the following scenarios:
- For databases systems, or if you use a lot of database intensive applications: Databases generally handle file caching way better than the OS would. If you reduce swappiness, you will limit the OS’s file caching, thus giving a chance to the database to handle its own caches.
- For interactivity: If you multitask a lot but handle few files or don’t open large documents, this could improve how smooth your system will feel. Less processes will be written out to disk, and as RAM access is much faster, your computer can feel faster. You must also bear in mind that this can reduce I/O performance. If you have slow disks or do anything I/O intensive, it might even hurt performance.
- For placebo: Many believe that a lower swappiness will speed up the system,. Seriously, f you think that your system is faster, it might have the effect of perceiving it to be faster, too.
Keep in mind that decreasing swappiness might result in crashes and processes being randomly killed by the system in order to free up memory. Decreasing swappiness will be best if you have enough RAM available to run your system smoothly. But you should keep the value above “10”, as some swap use is good to have.
Increasing swappiness can have the general advantage of speeding up I/O. Although not often advised, increasing swappiness might come in handy if:
- You perform some I/O intensive operations, and you have slow, old HDDs. For example: performing backups or batch editing images can be I/O intensive (A notable exception is databases which are naturally I/O intensive but could benefit from a lower swappiness value as seen above.).
- You have low memory but have relatively fast disks. In this unlikely scenario, a higher value of swappiness might help to handle memory more efficiently (although setting it too high might again hurt performance.).
How to manage swappiness
First, you should consider if you need to touch this value at all. Are you experiencing performance issues? And more importantly, have you tried other system tweaks yet? If not, it is probably best to look elsewhere first. But if you have a specific scenario in mind, you might want to continue.
Next, how heavy is your swap usage? You can find it out with any performance monitoring tool of your choice or with the
free -m will give you a snapshot of the memory usage in megabytes. For continuous monitoring, you might want to use
This will run the
free -m command every second and print its output until you press “Ctrl + C.”
As you see above, very little swap is being used by the system this article is written on.
Now if you do experience swapping, you might be interested how much of it is active. The command
vmstat will tell you all you need to know about your system’s virtual memory usage (swap and physical ram together).
You need to check the
swap column where
si means “swap in”, and
so means “swap out.” If the numbers are high, it means a lot of swapping activity which is an indicator of low memory issues. If you see swap usage by
free but little active swapping, tweaking swappiness might be due.
It is also a good idea to establish other performance benchmarks like disk I/O, load averages, etc., so when you test your new swappiness values, you have something to compare against.
To test a different swappiness value, you can set it temporarily with the
sysctl command. This needs no rebooting, and the effect is immediate. In fact, rebooting will restore the default value, so you are quite safe to experiment:
Of course, you can put any value instead of “10” (between “0” and “100”).
Once you’ve found your preferred value, you can permanently change the system configuration by editing “/etc/sysctl.conf”
and adding the lines
to the end of this file, with the value set to your preference once again, of course.
Lowering swappiness to “10” is often advised as a one-size-fits-all solution, but the actual use of the technique might be a tiny bit more complicated than that. By establishing benchmarks, knowing your system, how you use it, and what you need from it, you can fine tune your swap usage and achieve some performance (either interactivity or I/O) increase.
Get our stories delivered to your inbox.