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%:
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%.
swappiness=0
will wait to swap until it is absolutely necessary. Setting a moderate value like swappiness=10
will cause pages to be swapped from memory to disk somewhat more readily. This can prevent the need to swap a lot at once; such a need can introduce annoying delays.
In addition, often a process runs but does nothing for an extended time. Many daemons (background services) behave this way. You may have a background application that is left unused for a while. And these days some applications are implemented as multiple processes, like Chromium and Google Chrome, where each tab has a separate process behind it (not a separate thread, but a separate process). Setting swappiness
to a value like 10 makes it so these background tasks that are not being actively used are swapped to memory even when they could remain crammed in RAM. Then when a more actively used process needs to allocate more RAM, it can do so more quickly.
In conclusion, allowing processes to be swapped from RAM to disk earlier than necessary often confers a performance benefit and lower latencies when a process allocates memory. This is at the expense of the time it takes to swap the processes back. But this is usually done much less frequently than a process in active use allocates and releases memory, so the trade-off is often worthwhile.
Best Answer
Add that at the bottom without the
#
before it then run:Sample from my
sysctl.conf
file: