469

I need a step-by-step, simple and easy way to configure swappiness.

2
  • 25
    One liner sudo bash -c "echo 'vm.swappiness = 15' >> /etc/sysctl.conf" May 7, 2014 at 20:45
  • 17
    @redanimalwar you also need to run sudo sysctl -p to load the new swappiness value from the sysctl.conf file, otherwise the change just applies on the next reboot.
    – waldyrious
    Jun 29, 2015 at 14:16

3 Answers 3

668

The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness.

A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly.

Ubuntu system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:

one@onezero:~$ cat /proc/sys/vm/swappiness
60

As I have 4 GB of RAM I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent. To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 10

Apply the change.

sudo sysctl -p

You can also change the value while your system is still running with:

sysctl vm.swappiness=10

You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

To calculate your swap Formula:

free -m (total) / 100 = A

A * 10

root@onezero:/home/one# free -m
             total       used       free     shared    buffers     cached
Mem:          3950       2262       1687          0        407        952
-/+ buffers/cache:        903       3047
Swap:         1953          0       1953

so total is 3950 / 100 = 39.5 * 10 = 395

So what it mean is that when 10 % (395 MB) of ram is left then it will start using swap.


What is swappiness?

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

  • swappiness can have a value between 0 and 100.
  • swappiness=0:
    • Kernel version 3.5 and newer: disables swapiness.
    • Kernel version older than 3.5: avoids swapping processes out of physical memory for as long as possible.
  • swappiness=1:
    • Kernel version 3.5 and over: minimum swappiness without disabling it entirely.
  • swappiness=100:
    • Tells the kernel to aggressively swap processes out of physical memory and move them to swap cache.

See http://en.wikipedia.org/wiki/Swappiness.

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment.


Example

Started using swap at 91%:

enter image description here As I have configured my system & vm to make use of RAM at 90%, at 90% there was no swapping.

After that I opened some applications like Firefox & Shutter, and it started swapping because RAM usage is above 90%.

27
  • 156
    Swappiness of 100 does not make programs get swapped out immediately, nor does a value of 60 or 90 mean swap gets used when ram is 60 or 90% full. It is a ratio of preference for stealing pages from cache vs. swapping things out to free up some memory when there isn't enough. A low value will strongly prefer stealing pages from the cache, and a high value strongly prefers to try and swap pages out. The setting only has any effect once memory is ( nearly ) all used, and the kernel has to choose how to free some up.
    – psusi
    Feb 13, 2012 at 19:15
  • 79
    You need to point out that the RAM which is NOT occupied by running programs is used as disk cache... so, by decreasing swappiness, you increase the chance of a program not to be swapped out, but at the same time decrease the size of disk cache, which can make disk access slower. So the effects of this setting on the actual performance are not that straightforward... you're welcome to experiment of course but I suspect the defaults are set by people who understand enough in the subject :)
    – Sergey
    Feb 14, 2012 at 0:42
  • 23
    @Sergey and the irony of it is that those with small RAM are most likely to try every trick they can come across to boost performance and are more likely to be the people for whom 60 or even higher would be the best figure. Those of us setting it to 10 because we've tonnes of RAM aren't gaining as much as they'll lose if they do so.
    – Jon Hanna
    Aug 7, 2012 at 1:35
  • 11
    @Freedom_Ben: See linuxatemyram.com :)
    – Sergey
    May 14, 2014 at 7:16
  • 9
    Just leaving a similar discussion here - unix.stackexchange.com/questions/88693/… Nov 4, 2015 at 13:59
12

I need a step-by-step, simple and easy way to configure swappiness.

I'd like to expound upon the answers already here by adding:

  1. A very concise summary of how to change the swappiness.
  2. Some quotes from the latest Linux kernel source code (see here for how to download it if interested) on what "swappiness" really means, and what its ranges really are.

So, here it goes:

1. How to configure swappiness

# read current swappiness setting
sysctl vm.swappiness
# or (same thing)
cat /proc/sys/vm/swappiness

# change setting to zero
sudo sysctl vm.swappiness=0

The above commands are not persistent across reboots. To make the setting persistent, you need to use sudo and edit the file at /etc/sysctl.conf to add your desired swappiness setting to the end of it. Example:

# edit the file with the `nano` editor
sudo nano /etc/sysctl.conf

Add this to the end of "/etc/sysctl.conf":

# my custom swappiness setting
vm.swappiness=0

If you choose to only edit that file to set your custom swappiness setting, rather than setting it with sudo sysctl vm.swappiness=0, then to activate your new setting as set in the file, you will need to either reboot or call the following command to reload the config file:

# reload just the "/etc/sysctl.conf" config file
sudo sysctl --load

# or: from `man sysctl`:
#
# Load settings from all system configuration files, namely:
#
#       /run/sysctl.d/*.conf
#       /etc/sysctl.d/*.conf
#       /usr/local/lib/sysctl.d/*.conf
#       /usr/lib/sysctl.d/*.conf
#       /lib/sysctl.d/*.conf
#       /etc/sysctl.conf
#
sudo sysctl --system

At the end of running either of the above commands, the terminal will print this to stdout:

vm.swappiness = 0

To see what your current settings are in /etc/sysctl.conf:

cat /etc/sysctl.conf

2. What is swappiness, straight from the Linux kernel developers' mouths

Download the Linux source code yourself by following my instructions here: Where and how to get the official Linux kernel source code.

As shown in the Linux Stable repo by git blame Documentation/admin-guide/sysctl/vm.rst, this documentation was written by Peter W Morreale, Mauro Carvalho Chehab, and Johannes Weiner (see the kernel source code here) (emphasis added, & formatting slightly modified):

swappiness

==========

This control is used to define the rough relative IO cost of swapping and filesystem paging, as a value between 0 and 200. At 100, the VM assumes equal IO cost and will thus apply memory pressure to the page cache and swap-backed pages equally; lower values signify more expensive swap IO, higher values indicates cheaper.

Keep in mind that filesystem IO patterns under memory pressure tend to be more efficient than swap's random IO. An optimal value will require experimentation and will also be workload-dependent.

The default value is 60.

For in-memory swap, like zram or zswap, as well as hybrid setups that have swap on faster devices than the filesystem, values beyond 100 can be considered. For example, if the random IO against the swap device is on average 2x faster than IO from the filesystem, swappiness should be 133 (x + 2x = 200, 2x = 133.33).

At 0, the kernel will not initiate swap until the amount of free and file-backed pages is less than the high watermark in a zone.

So, values do not range between 0 and 100 anymore. As of this commit hash c843966c556d7 on Jun 3, 2020, the value now ranges from 0 to 200.

The commit hash 497a6c1b09902b22ceccc0f25ba4dd623e1ddb7d right before that stated this instead:

swappiness

==========

This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness, 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.

The default value is 60.

The default swappiness value has been 60 going back at least to Linux kernel version v2.6.12-rc2, 18 years ago (as of 2005), as shown by git blame in the mm/vmscan.c file where it is set, here.

Which swappiness should I use?

I highly recommend vm.swappiness=0! It makes my machine run soooo much better. From my anecdote in my answer here:

I have found that setting swappiness to 0 significantly improves the performance of my system which has 32 GB RAM, a 64 GB swap file on a high-speed m.2 SSD, and which is continually running out of RAM.

With swappiness set to the default of 60, I'd regularly get 1 to 2 minute lockup periods while kswapd0 is running (as shown by top) to try to swap memory for some memory hog application like Chrome, Slack, Eclipse, or Google Meet (within Chrome). I'd start to get these lockups at 80% full RAM. The computer would be completely unusable during this time--unable even for me to type into a terminal or click on a menu.

Setting swappiness to 0 significantly helped!. I started not getting really high CPU usage until 90% RAM full, swap space would still get used plenty--but more efficiently, and when my RAM did get almost full my computer would become very sluggish, but still barely usable rather than completely unusable!

See some of my symptoms here, which I originally thought were due to a bug in Google Meet, but now think were due to memory swap making my computer slow: https://github.com/ElectricRCAircraftGuy/bug_reports/issues/3#issue-1177137900

On my system:

  • With vm.swappiness=60, I'd see 1 to 2 minutes of 100% CPU lockup starting at about 79% RAM usage, every 4 to 6 minutes, forever. This was particularly exacerbated and prominent when using Google Meet.
  • With vm.swappiness=0, that wouldn't happen. I'd be fine at 80% RAM usage, and would start to see major sluggishness, but not total lockup like before, by 90% RAM usage.
    • Even with swappiness set to 0, I'd see some usage (a few MB) of swap used as early as 0 to 3% of RAM used, and I'd have heavy swap usage by 80% to 90% RAM used.

References:

  1. https://linuxize.com/post/how-to-change-the-swappiness-value-in-linux/ - where I learned most of the sysctl cmds above
  2. https://www.cyberciti.biz/faq/reload-sysctl-conf-on-linux-using-sysctl/ - where I learned the sysctl --system cmd above

Related:

  1. kswapd0 is taking a lot of cpu - useful, but this quote is totally wrong though:

    where 0 is the percent left out of 100 at which SWAP should be used (when you have 0% RAM left, SWAP will start taking in data).

  2. My answers--I needed this swappiness info. for them:

    1. *****Very useful--I reference this one a lot: Problem: Eclipse and the Eclipse indexer take up all my resources / CPU%
    2. Unix & Linux: what is the different between settings swappiness to 0 to swapoff
  3. My answer: How do I increase the size of swapfile without removing it in the terminal?

3

For ZRAM swap without any actual swap partitions/files, use 100. It'll pre-compress everything what can be pre-compressed, leaving cache intact and quickly decompress data as needed (also, without real swap you'll need to increase admin_reserve_kbytes x2 or even x4 to avoid whole system freezing on low RAM instead of dropping a hungry application).

For SSD with actual swap partition, use 1. It'll prevent from swapping as long as possible, sacrificing the cache (but cache can be easily re-read from SSD).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .