How to permanently change swappiness from 60 to value?

man sysctl.d

CONFIGURATION DIRECTORIES AND PRECEDENCE

All configuration files are sorted by their filename in lexicographic order

It is recommended to prefix all filenames with a two-digit number and a dash to simplify the ordering.

If there is only one or two files in /etc/sysctl.conf.d/, there is no need to “simplify the ordering”

That’s interesting, I just checked mine - swappiness was 60, but the file was set to 35 (I think a good number for 16 GiB)…

1 Like

Thanks.

To summarize, I retain:
Create a .conf file into:
/etc/sysctl.d/
and apparently no more into /etc/
Any name will do.

As I said, it’ll help, that a swap partition creation, to ask for the swappiness, because the default 60 is very different if you have 8Gb or 32Gb and HDD or SSD.

Now, if you have:
/etc/sysctl.d/10-swappiness.conf
/etc/sysctl.d/20-swappiness.conf
/etc/sysctl.d/30-swappiness.conf

then it’s even more confusing, because you wonder which one is active and how you know that.

man sysctl.d

CONFIGURATION DIRECTORIES AND PRECEDENCE

All configuration files are sorted by their filename in lexicographic order, regardless of which of the directories they reside in. If multiple files specify the same option, the entry in the file with the lexicographically latest name will take precedence. Thus, the configuration in a certain file may either be replaced completely (by placing a file with the same name in a directory with higher priority), or individual settings might be changed (by specifying additional settings in a file with a different name that is ordered later).

In principle, the figures represent priorities.
10 → Low priority
30 → High priority

Run this:

grep -R '.' /etc/sysctl.d/*

It is read in exactly in this order as one file. If there are several variables, the newest is overwritten with the oldest.

Let’s say, you have:

/etc/sysctl.d/10-swappiness.conf:vm.swappiness = 80
/etc/sysctl.d/20-swappiness.conf:vm.swappiness = 40
/etc/sysctl.d/30-swappiness.conf:vm.swappiness = 30

Then the second value overwrites the first and the third overwrites the second, so that only the third value comes into effect.

The result is read with:

sudo sysctl -a | grep "vm.swappiness" 

Conky to the rescue…

It seems you (still) misunderstand what that value means.
It’s not a percentage of the size of the swap space - it is used by the kernel to calculate how readily swap is utilized in favor of RAM.

If you have that, you had to have done it yourself - and if you find that confusing, you confused yourself by doing it that way. :man_shrugging:
It’s a pretty contrived example …

But if you follow your example and create these files, each one containing a different, step by step higher value, then you can see for yourself which value / which file ends up getting used.

Solution:
If these files contain the same key
vm.swappiness in this case
but a different value for that key,
the value in the file name with the highest number will end up being used.

1 Like

Swap - Swappiness - ArchWiki

The swappiness sysctl parameter represents the kernel’s preference for writing to swap instead of files. It can have a value between 0 and 200 (max 100 if Linux < 5.8); the default value is 60. A low value causes the kernel to prefer freeing up open files, a high value causes the kernel to try to use swap space, and a value of 100 means IO cost is assumed to be equal.

Note: There is a common misconception that swappiness affects the memory threshold or prevents using swap space, but it only affects the preference for freeing up file pages over swap. See: this article for a more detailed explanation or the kernel source code where it is used.

To check the current swappiness value:

$ sysctl vm.swappiness

Alternatively, the file /proc/sys/vm/swappiness can be read in order to obtain the raw integer value.

cat /proc/sys/vm/swappiness
4 Likes