How to Permanently Disable Swap Partition in Linux

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 

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Matei Cezar
I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

10 Comments

Leave a Reply
  1. With modern PCs, sporting 8, 16, or more GB of RAM, swap partition/file has become unnecessary. So, instead of just disabling it, delete it.

    Granted that the gain in available space will be minimal but deleting swap will leave one less partition to be possibly corrupted.

    One caveat with deleting swap is that some distros may refuse to install unless some kind of swap is present.

    Reply
    • @dragonmouth,

      Thank you for sharing your perspective on the topic of swap partitions/files in the context of modern PCs with ample RAM. You’re right that with the increasing amounts of RAM in today’s computers, the traditional role of swap as an “overflow” for RAM is less critical than it once was.

      Reply
  2. I’ve been told that “swap space” is used during SUSPEND operations.

    I welcome someone to offer a robust explanation. It is important for road warriors and any who rely on laptop portability.

    During “sleep” [suspend to ram] current runtime details get written to swap before putting things on hold.

    During “hibernate” [suspend to disk] the majority of the system state gets written to swap before power off.

    Reply
  3. The “free -h” commands must be executed when the system is under heavy load. Otherwise, it does not provide the information necessary to determine whether a swap partition or space is needed. When executed on an idling system, the command will only tell you the size of the swap partition and that it is not being used. It’s like running the “top” command on an idling system to see the CPU Load. It will show the CPU Load to be negligible.

    I agree with S. Daniels on the use of GParted. It not only provides a visual representation of your disks but also can delete your swap partition and recover that space by moving and/or resizing other partitions. Using the command line to accomplish your tasks may be ‘leet’ and cool but it is also dangerous to the health of your system. Using GParted prevents misspelling or use of incorrect CLI commands, as well as the use of incorrect options which can result in making the drive unusable.

    Reply
  4. These instructions are rather outdated for any linux with a desktop:MATE, xfce, Gnome or KDE/Plasma. Firstly, the program Gparted very nicely displays swap size, location and UUID. “blkid”, especially on a dual-boot or system with multiple OSes is just going to display a lot of confusing and unhelpful entries.

    The KDE partitioner is inferior, but adequate. The centos-swap is fairly specific to CentOS Linux, this should be explained in the article. A swap file can also be used in place of a dedicated partition, this should also be mentioned.

    “Vi” is very much overkill for editing fstab. “Vim” is easier than “vi” for newbies in every case, and “pico /etc/fstab” or “nano /etc/fstab” are more appropriate here.

    Finally, for José , 30GB is seriously wrong. I suspect he somehow changed a data or system partition into a swap partition. He can “manage flags in Gparted” to inspect it for contents.

    I’m just a hobbyist, but have nearly 20 years experience, since a Debian bootable CD, the first Linux installation disk that did not need a floppy to boot, came out in 2000.

    Reply
  5. If there is sufficient RAM (8GB), computer may never access swap space. What is the benefit of disabling swap in this case? Conversely, how will swap cause problems if the computer never accesses it.

    Reply
  6. In my case, I have 30 GB. free 0 GB. I should not disable?

    total used free shared buff/cache available
    Memoria: 7.7G 1.6G 4.6G 196M 1.5G 5.6G
    Swap—–: 30G 0B 30G

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.