How to disable swap in Linux

Besides the RAM there is a so-called Swap, which is a virtual memory, where RAM content (pages) could be swapped-in in case there is not enough RAM available anymore. It could be a disk partition, Logical volume, or even a file. This swap is located on the disc and since disc reads and writes are slower than reading from RAM, accessing memory pages there will result in a delay.

The cache in Linux is used to speed up reads from disc – content which has been read once from disc is kept in memory, so that if there is a request for the same content later again, it has not to be read again from the disc, but taken from memory. This is much faster than reading it from the disc. The cache usually uses all free memory (fills up slowly as files are read from disc). The RAM used for cache could be freed at any time if a program requests additional memory and there is no unused (free in atop, top and ps) RAM anymore.

However, it could make sense to keep the cached files in the RAM and satisfy the request for additional memory by swapping (writing memory pages from the RAM into the slower Swap on disc) some currently not used memory pages of other running processes. This makes especially sense when you often have to read the same files again and again (for example with databases), to keep the fast file access performance.

On Linux systems, the so-called swapiness defines what should be done in case there is no unused memory anymore and a program requests more memory, whether it should be preferred to swap out memory pages currently not needed of other programs or to release memory from the cache.

Check swap configuration

To check if swap is on or off, run “swapon -s” from the shell interface. If this command returns a non-empty output than the swap is on, for example:

# swapon -s
Filename                Type         Size       Used    Priority
/dev/sda1               partition    7999484    1201556    0

Here is how the output looks like if swap is off:

# swapon -s
Filename                                Type            Size    Used    Priority
#

Disabling swap temporarily

For testing whether a certain issue might be influenced by the usage of the swap, you might turn off the swap temporarily. This action takes effect immediately, the content of the swap is written to the RAM. The action stays in place till swapon -a is executed or the system is rebooted.

Deactivate the swap:

Login via ssh to the system and run the below command:

# swapoff -a

Activate the swap again:

Login via ssh to the system and run the below command:

# swapon -a

Disabling permanently

Login via ssh and in /etc/fstab comment the line by setting a “#” in front:

UUID=347569f8-88d2-4ad1-8a04-01eacd2cd4ba    swap    swap    defaults    0    0
Note: The UID might be different in your case, important is the “swap” entry.

To trigger the change, use one of the options:
1. type swapoff -a
2. reboot (by entering “reboot”)

To double check the current state, use command swapon -s:

# swapon -s
Related Post