How disable swap in debian or linux system
swappiness
Swappiness is a Linux kernel parameter that controls the relative weight given to swapping out runtime memory, as opposed to dropping pages from the system page cache . Swappiness can be set to values between 0 and 100 inclusive. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may decrease response latency.
Swappiness
The default value of vm.swappiness is 60 and represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory.VALUE | STRATEGY |
---|---|
vm.swappiness = 0 | Version 3.5 and over: disables swapping. Prior to version 3.5: The kernel will swap only to avoid an out of memory condition. |
vm.swappiness = 1 | Version 3.5 and over: Minimum swappiness without disabling it entirely |
vm.swappiness = 60 | The default value. |
vm.swappiness = 100 | The kernel will swap aggressively. |
To temporarily set the swappiness in Linux, write the desired value (e.g. 10) to /proc/sys/vm/swappiness using the following command, running as root user:
# Set the swappiness value as root
echo 10 > /proc/sys/vm/swappiness
# Alternatively, run this as a non-root user
sysctl -w vm.swappiness=10
# Verify the change
cat /proc/sys/vm/swappiness
10
# Alternatively, verify the change
sysctl vm.swappiness
vm.swappiness = 10
Disable linux swap permanently
# edit /etc/sysctl.conf file
~] vi /etc/sysctl.conf
# disable swap
# add to end of file if not present:
vm.swappiness = 0
# reboot for changes
systemctl reboot
# ...
# ...
# ...
vm.swappiness = 0
Live changes in swappiness file
Here's the output of running vmstat 1, where you can see my machine is not swapping at all.
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 78588 230788 9596 72196 0 0 0 0 543 652 12 6 78 0
0 0 78588 230780 9596 72196 0 0 0 0 531 410 1 0 99 0
0 0 78588 230796 9596 72196 0 0 0 0 300 335 1 1 97 0
1 0 78588 230788 9608 72224 0 0 40 0 737 762 4 4 84 8
5 0 78588 230788 9608 72224 0 0 0 0 415 385 9 3 84 0
0 0 78588 230540 9616 72224 0 0 0 44 611 556 55 5 31 0
0 0 78588 230532 9616 72224 0 0 0 0 574 662 1 6 89 0
Yet here in top command you can see I have swap space allocated:
Mem: 475236k total, 245076k used, 230160k free, 9720k buffers
Swap: 491512k total, 78588k used, 412924k free, 72476k cached
And live changes:
# disable swap
~] swapoff -a
# To set the new value to 20:
# make changes as root
~] echo 20 > /proc/sys/vm/swappiness
# To turn swap back on:
~] swapon -a