How do I configure swappiness?
Solution 1
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.
Help . Ubuntu . 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%:
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%.
Solution 2
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).
Related videos on Youtube
One Zero
Updated on September 18, 2022Comments
-
One Zero over 1 year
I need a step-by-step, simple and easy way to configure swappiness.
-
redanimalwar about 10 yearsOne liner
sudo bash -c "echo 'vm.swappiness = 15' >> /etc/sysctl.conf"
-
user2390005 almost 9 years@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.
-
-
psusi over 12 yearsSwappiness 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.
-
Sergey over 12 yearsYou 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 :)
-
Jon Hanna almost 12 years@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.
-
mchid about 10 yearsYeah, I did this on an older computer with only 1/2Gb of ram and couldn't figure out what was constantly grinding my hard drive to a halt. Using iotop I figured out the computer was searching for needed swap that did not exist. There's way less hard drive activity now that I set it back to 60 and it runs gnome-shell just fine without getting bogged down.
-
Freedom_Ben about 10 years@Sergey do you have a source/reference about unused RAM being used as disk cache for further reading on the subject?
-
Sergey about 10 years@Freedom_Ben: See linuxatemyram.com :)
-
rubo77 almost 10 yearsYou can read the actual value with just
sysctl vm.swappiness
-
CMCDragonkai almost 10 yearsWhat if your filesystem cache is managed by ZFS which has their own processes?
-
h3. over 9 yearsThat's a good explanation for the most part, but the recommendation to change the default swappiness is counter-productive. Making the computer swap less usually causes more I/O because it keeps reloading useful data that it couldn't keep in the cache.
-
Elijah Lynn almost 9 yearsHow does one tell what version of swapiness they have? >= 3.5
-
Elijah Lynn almost 9 yearsAhh, it is kernel version 3.5+, originaly source here percona.com/blog/2014/04/28/…
-
mondjunge almost 9 yearsMy experience with swappiness 10, compared to swappiness 60 on ubuntu 14.04 with 8GB is: I got better response times. Eclipse is not freezing as often as it used to. Unity freezes and complete lockups do not occure anymore (they did on a daily basis before). My Disk is working much less, with swappiness 60 I had fulltime diskoperations as soon as more than 4GB of Ram were used. BUT: I think as a JAVA developer I am not the intended targetgroup for default setting. No setting is default in my ubuntu setup. Be careful when changing yours.
-
Elijah Lynn over 8 yearsJust leaving a similar discussion here - unix.stackexchange.com/questions/88693/…
-
killermist over 8 yearsIt is also worthy of note that what kind of media on which the swap is stored and how much other traffic the swap device sees can have HUGE affects on performance. If your swap is stored on rotational media (HDD) with the running OS, you probably want to swap as little as possible. If on the other hand, swap is stored on SSD (with or without the running OS), the combination of huge numbers of IOPS and zero seek time means that swap is vastly more responsive, so using more is fairly harmless. Not as fast as ram, but not nearly as bad as rotational media.
-
Luke Maurer over 8 years@killermist But at least the conventional wisdom is that swapping is bad for the lifetime of an SSD; it's typical for tutorials to suggest setting swappiness at 1 for any SSD. Do you know if that's true? (Conventional wisdom says a lot of things …)
-
jerclarke about 8 years@LukeMaurer Here's another answer about whether to minimize swappiness on SSDs askubuntu.com/questions/652337/… #TLDR Applies mostly to early SSD, shouldn't be necessary with modern SSD.
-
adf88 over 7 yearsInstead of modifying system file "/etc/sysctl.conf" perhaps it's better to write into some "/usr/local/lib/sysctl.d/*.conf" file.
-
Geremia about 7 years@Sergey vw.swappiness=0 doesn't necessarily mean less disk cache (e.g., in the case when there's much free RAM it doesn't).
-
Sergey about 7 years@Geremia: If you have a lot of free RAM (not used by programs nor disk cache) then even with high swappiness value the programs won't be swapped out. They're only swapped out when kernel thinks there's better use for that RAM than keeping code/data which is not currently needed. I have 32Gb of RAM on this machine and, despite tons of programs open, still zero swap usage (swappiness=60).
-
d4nyll over 6 years"As I have 4 GB of RAM, so I'd like to turn that down to 10 or 15." <---- say that again in 2018. Half of that is used for running Slack on Ubuntu.
-
Chris1804505 over 5 yearsAs @Sergey pointed out correctly, your 'free' RAM is actually not wasted, but used for disk cache. Meaning it's perfectly fine in some cases to have a balance between some swapped memory and the free RAM (which is used for disk cache). Unless you have really a lot of RAM in your system, and you think all the free RAM is not occupied for disk cache. Therefor your actually free in 'free -m' column can be close to zero, which is fine.
-
user228505 over 4 yearsAs others have already stated, this answer is wrong. It should not be marked as the accepted answer. Swappiness controls the ratio between swapping file backed pages vs anonymous memory. For a deeper technical discussion about swapping especially with fast i/o devices like SSD or even ZRAM this patch-series and the discussion around it is interesting: lore.kernel.org/patchwork/patch/685711
-
Dan Dascalescu over 4 yearsFor a laptop with 8GB RAM and a fast SSD, setting
swappiness
to 10 made Ubuntu 18.04 be MUCH SMOOTHER, similar to @mondjunge's experience. No more GNOME freezes, no more mouse cursor skipping when the OS would thrash the swap file. The top apps I run (in terms of resources) all the time are Chrome with 50+ tabs and the WebStorm IDE (a Java application). -
userDepth over 3 yearsSo swapiness 60 means that when my RAM is 40% it starts to use swap?
-
DJCrashdummy about 2 years@userDepth no, as you can see here. please also read the comments before adding one, especially such highly upvoted ones!