What I usually do is create a custom rule in /etc/sysctl.d/
instead of editing the main sysctl
file. Change directory to sysctl.d
and create your file:
cd /etc/sysctl.d/
sudo touch 60-my-swappiness.conf
Then run your favourite text editor:
sudo nano 60-my-swappiness.conf
Enter your value at the top of the file, for example, vm.swappiness = 10
(there must be spaces between swappiness and the value as shown).
As it recommends in the readme in /etc/sysctl.d/
, run
sudo service procps start
for the system to read the new values and then reboot.
The reason why we use 60
at the start of the my-swappiness.conf
is so that your custom rule overrides any other rules in the directory.
Check that you have the correct swappiness with
cat /proc/sys/vm/swappiness
10
This works for me, but there is more guidance on swap and swappiness in the Ubuntu guide if you need any more detailed information.
please verify first by using the following:
cat /proc/sys/vm/swappiness
This output gives you the right swappiness at the moment. (it could be 0 it could be 60)
If you want to change this do the following:
gksudo leafpad /etc/sysctl.conf
I use leafpad as an editor but you can change this with gedit (what has a bug in it)
scroll at the bottom of this file and add
# altering swappiness
vm.swappiness= (give here the digit you want e.g. 60 or 10 (bold is for pointing out)
save this altered file and reboot completely
verify your swappiness again by using the command I gave above (cat ....)
I use for 1Gb RAM or more the value 10; for less than 1Gb RAM the value 5 (personal experience) . Only for a server or something like that the value 60 is required.
Good luck !
Best Answer
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:
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:Apply the change.
You can also change the value while your system is still running with:
You can also clear your swap by running
swapoff -a
and thenswapon -a
as root instead of rebooting to achieve the same effect.To calculate your swap Formula:
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
:swappiness=1
:swappiness=100
: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 ofswappiness=10
is recommended, but feel free to experiment.Example
Started using swap at 91%:
After that I opened some applications like Firefox & Shutter, and it started swapping because RAM usage is above 90%.