What is Swappiness?

Most Linux users that have installed a distribution before, must have noticed the existence of the “swap space” during the partitioning phase (it is often found as /sda5). This is a dedicated space in your hard drive that is usually set to at least twice the capacity of your RAM, and it constitutes the total virtual memory of your system. The swap space can also be provided by a so-called swap-file, which you can use in case you can not create a dedicated swap partition. In general, swap partitions are preferred over swap files speed wise though. From time to time, the Linux kernel utilizes this swap space by copying chunks from your RAM to the swap, allowing active processes that require more memory than it is physically available to run.

Swappiness is the kernel parameter that defines how much (and how often) your Linux kernel will copy RAM contents to swap. This parameter's default value is “60” and it can take anything from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively your kernel will swap.

Why change it?

The default value is a one-fit-all solution that can't possibly be equally efficient in all of the individual use cases, hardware specifications and user needs. Moreover, the swappiness of a system is a primary factor that determines the overall functionality and speed performance of an OS. That said, it is very important to understand how swappiness works and how the various configurations of this element could improve the operation of your system and thus your everyday user experience.

As RAM memory is so much larger and cheaper than it used to be in the past, there are many users nowadays that have enough memory to almost never need to use the swap file. The obvious benefit that derives from this is that no system resources are ever occupied by the swapping process and that cached files are not moved back and forth from the RAM to the swap and vise Versa for no reason.

How to change Swappiness?

The swappiness parameter value is stored in a simple configuration text file located in /proc/sys/vm and is named “swappiness”. If you navigate there through the file manager, you will be able to locate the file and open it to check your system's swappiness. You can also check it or change it through the terminal (which is faster) by typing the following command:

sudo sysctl vm.swappiness=10

or whatever else between “0” and “100” instead of the value “10” that I used. To ensure that the swappiness value was correctly changed to the desired one, you simply type:

cat /proc/sys/vm/swappiness

on the terminal again and the active value will be outputted.

Linux Swappiness

This change has an immediate effect on your system's operation and thus no rebooting is required. In fact, rebooting will revert the swappiness back to its default value (60). If you have thoroughly tested your desired swapping value and you found that it works reliably, you can make the change permanent by navigating to /etc/sysctl.conf which is yet another text configuration file. You may open this as root (administrator) and add the following line on the bottom to determine the swappiness: vm.swappiness="your desire value here". Then, save the text file and you're done!

Set swappiness value in Linux

Factors for consideration

There are some maths involved in the swappiness that should be considered when changing your settings. The parameter value set to “60” means that your kernel will swap when RAM reaches 40% capacity. Setting it to “100” means that your kernel will try to swap everything. Setting it to 10 (like I did on this tutorial) means that swap will be used when RAM is 90% full, so if you have enough RAM memory, this could be a safe option that would easily improve the performance of your system.

Some users though want the full cake and that means that they set swapping to “1” or even “0”. “1” is the minimum possible “active swapping” setting while “0” means disable swapping completely and only revert to when RAM is completely filled. While these settings can still theoretically work, testing it in low-spec systems of 2GB RAM or less may cause freezes and make the OS completely unresponsive. Generally, finding out what the golden means between overall system performance and response latency requires quite some experimentation (as always).

Share this page:

Suggested articles

21 Comment(s)

Add comment

Comments

By: JustNiz at: 2015-06-22 22:57:34

it seems to me you'd always want to use RAM as much as possible first, So why would you ever want to set swappiness so you leave more free ram?

By: Albin at: 2015-06-23 11:41:13

Swappiness is an important topic for server implementations of Linux, but not for normal desktop use.  There are a lot of "urban myth" recommendations for desktop, but all the serious discussions relate to servers.  The only desktop machines that will normally swap at all are very old low power units, e.g. under 1mb of RAM, where the combination of the OS and an ordinary browser is topping out the RAM.  Apart from that, any system monitor will show you that any desktop machine with normal specs isn't swapping, period.

By: some dude at: 2015-10-31 09:11:42

Well, I certainly have more than 1MB or RAM, 2 GB, which I deem to be a somewhat "normal" spec, however in the low end, and my OS certainly swaps, sometimes approaching the partition limit. Not so much "by itself", but yes, when lots of stuff are using memory, including but not limited to web browsers.

I don't really monitor it that cautiously, but I've basically noted it always happening to some degree at least. I've also tried somewhat haphazardly several swappiness configurations. Lately I have been using "2", which still swaps. 

 

I think low swappiness may have the downside of every now and then frozing the system in a more dramatic manner than with more swapping, which would perhaps spread the swapping into smaller slices over time, with shorter responsivity declines.

By: JFM at: 2015-06-23 13:02:28

Files are not wriitten to swap.  If the block is clean it is silently discarded if it is dirty it is written to the emplecament it it should occuppy in the file not to swap.

 

@Albin.

Try just try to edit a one hour 24 bit, 192000 Khz one hour audio file with audacity on a 4GB box without swap.  Even with it, it can be painful.

@JustNiz

 

Because it is better to put off a fire when it is still small.  Swapping pages who are little used or perhaps will no longer be reused at all (like fo intialisation routines) when machine is under no pressure is better than swapping them when OS neeeds to free hundreds of megs of RAM

 

Also if swappiness is too low (or you have no swap) Linux will discard library (read/only pages) possibly having to reload them two miliseconds later.

 

By: JFM at: 2015-06-23 15:11:37

@JustNiz again

Another reason for swapping is providing more space more for file caching.  It is better to swap pages that are very unfrequently unused than read and reread and reread the same block belonging to a file.

By: JustNiz at: 2015-06-23 16:57:32

JFM Thanks for taking time to expalain. I get it now.

By: mickeypop at: 2015-06-23 20:33:17

a simple script to capture memory usage over time and maybe plot it later can help alot to find out just how much you use and where swapping is actually needed.  Then setting the swappiness would generally have the best results. 

a simple script to show ram and swap use may help

#!/bin/bash /usr/bin/free -b | /bin/awk ' \ NR==2 { ramUsed = $3 } \ NR==4 { swapUsed = $3 } \ END { print swapUsed "\n" ramUsed "\n0\n0" } '

hope this can hepl someone

 

 

By: Dan Saint-Andre at: 2015-06-23 21:43:02

In these days of 16+ GB of system RAM, does "twice RAM" or similar really make sense as the size of swap partitions?

As I understand things, the swap partition is important for suspend-to-RAM(SLEEP) and suspend-to-disk(HIBERNATE) operations.

By: Albin at: 2015-06-24 14:38:35

@JFM

Actually the "urban myths" I mentioned have mostly to do with reducing swappiness to 10 or whatever, to reduce something that usually isn't happening anyway.  I don't know Audacity, but most a/v media software has pretty sophisticated cache settings that can make a real difference to performance on large files, allocating various caches to a big fast hard drive, especially a separate hard drive, from the software itself doesn't require monkeying with the operating sytem settings.   

By: g joe at: 2015-11-15 06:45:28

Hi!

On my ubuntu 14.04 LTS in the /etc/sysctl.conf the vm.swappiness string is not present. Where I could set this value?

Thanks 

By: Emin at: 2018-05-27 12:48:03

That's normal, you need to open that file as root first (to be able to save changes), and add that line to the end (or any empty line) and save. For example go to the last line and paste

vm.swappiness = 10

By: Tom Shailer at: 2016-10-14 18:32:10

Great post! You can also monitor swap usage using free or top commands.

By: Anonymous at: 2016-10-23 20:30:23

This tutorial is wrong.  It says to edit this file /etc/sysctl.conf

Not for nothing but the actual path is /etc/sysctl.d/99-sysctl.conf

You should correct that, now

By: DerekVS at: 2017-03-02 17:33:44

@Anonymous,

/etc/sysctl.d/99-sysctl.conf is just a symlink to ../sysctl.conf, so the author is still correct.

By: Mario Tello at: 2017-11-07 19:17:05

in which you base that theory to say that it is a percentage.

Quote:

"The parameter value set to “60” means that your kernel will swap when RAM reaches 40% capacity. Setting it to “100” means that your kernel will try to swap everything. Setting it to 10 (like I did on this tutorial) means that swap will be used when RAM is 90% full, so if you have enough RAM memory, this could be a safe option that would easily improve the performance of your system."

 

This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase agressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

 

Source https://www.kernel.org/doc/Documentation/sysctl/vm.txt

Other factors influence the decision to move memory pages to swap

By: Anonymous at: 2019-07-10 18:49:54

$ ls -ld /etc/sysctl.d/99-sysctl.conf

lrwxrwxrwx 1 root root 14 May 25 03:58 /etc/sysctl.d/99-sysctl.conf -> ../sysctl.conf

By: Vishal Parkar at: 2019-11-20 07:42:58

Thanks !

By: Lucio at: 2020-02-12 19:05:52

Great tutorial, still, every now and then the value comes back to the default one (maybe after apt upgrade). I had to edit a cron entry to reset it to the value i like to use. Is there another approach recommended besides a cron?

By: Milind Palav at: 2020-03-07 05:17:27

For permanent swappinees value, you can insert following parameter in /etc/sysctl.conf

vm.swappiness=10

And to take effect without reboot run following command

sysctl -p

By: Tony Albers at: 2020-08-12 08:58:45

For others stumbling onto this:

/etc/sysctl.conf is not a static file, it can be overwritten by updates or when adding/removing software. Instead you should use /etc/sysctl.d

Set the swappiness permanently like so:

1. Create /etc/sysctl.d/100-swappiness.conf

2. If you want swappiness set to i.e.5, put this into the file: vm.swappiness=5

This will not be changed by apt/yum/whatever update. 

By: Jahf at: 2022-04-14 23:55:41

 For anyone looking at this now, the behavior of "swappiness 0" changed and no longer explicitely disables swap. It just makes it only swap when RAM is full and more is needed. You'll need to "swapoff" to fully disable (not recommending this). Most of the rest of the info is still useful, just be aware of the change.