Swapping or swap space represents a physical memory page that lives on top of a disk partition or a special disk file used for extending the RAM memory of a system when the physical memory fills up.

Using this method of extending RAM resources, inactive memory pages are frequently dumped into the swap area when no RAM is available. However, due to the spinning speed of classical hard disks, swap space is way lower in transfer speeds and access time compared to RAM.

On newer machines with fast SSD hard disks, reserving a small partition for swapping can greatly improve access time and speed transfer compared to classical HDD, but the speed is still magnitudes lower than RAM memory.

Some suggest that the swap space should be set as twice the amount of machine RAM. However, on systems with more than 4 GB of RAM, swap space should be set between 2 or 4 GB.

In case your server has sufficient RAM memory or does not require the use of swap space or the swapping greatly decreases your system performance, you should consider disabling the swap area.

How to Check Swap Space in Linux

Before actually disabling swap space, first, you need to visualize your memory load degree and then identify the partition that holds the swap area, by issuing the below free command.

# free -h 

Look for the Swap space used size. If the used size is 0B or close to 0 bytes, it can be assumed that swap space is not used intensively and can be safely disabled.

Check Swap Space
Check Swap Space

How to Check Swap Partition in Linux

Next, issue following the blkid command, look for TYPE="swap" line in order to identify the swap partition, as shown in the below screenshot.

# blkid 
Check Swap Partition Type
Check Swap Partition Type

Again, issue the following lsblk command to search and identify the [SWAP] partition as shown in the below screenshot.

# lsblk
Search Confirm Swap Partition
Search Confirm Swap Partition

How to Disable Swap in Linux

After you’ve identified the swap partition or file, execute the below command to deactivate the swap area.

# swapoff /dev/mapper/centos-swap 

Or disable all swaps from /proc/swaps, which provides a snapshot of the swap file name.

# swapoff -a 

Run free command in order to check if the swap area has been disabled.

# free -h
Disable Swap Partition
Disable Swap Partition

How to Disable Swap Permanently in Linux

In order to permanently disable swap space in Linux, open /etc/fstab file, search for the swap line and comment on the entire line by adding a # (hashtag) sign in front of the line, as shown in the below screenshot.

# vi /etc/fstab
Disable Swap Partition Permanently
Disable Swap Partition Permanently

Afterward, reboot the system in order to apply the new swap setting or issue mount -a command in some cases might do the trick.

# mount -a

After the system reboot, issuing the commands presented at the beginning of this tutorial should reflect that the swap area has been completely and permanently disabled in your system.

# free -h
# blkid # lsblk 

Similar Posts