Comments on How to change the Swappiness of your Linux system
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.
21 Comment(s)
Comments
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?
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.
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.
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.
@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.
JFM Thanks for taking time to expalain. I get it now.
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
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.
@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.
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
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 = 10Great post! You can also monitor swap usage using free or top commands.
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
@Anonymous,
/etc/sysctl.d/99-sysctl.conf is just a symlink to ../sysctl.conf, so the author is still correct.
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
$ 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
Thanks !
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?
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
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.
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.