In this tutorial, AZDIGI will show you how to disable Swap in Linux.
I. What is Swap?
Swap, also known as virtual RAM, is used to support storing data in hard disk memory when physical memory (RAM) is full. Sometimes Swap is also used in parallel to increase cache capacity even if the physical memory is not used up yet. Swap is usually used by default on Linux, Ubuntu or CentOS operating systems.
II. When should we use Swap?
One of the most important situations where a Swap is needed is when your system is running out of physical memory (RAM).
It can be understood simply: you can never predict the amount of memory for all programs on the operating system. So when the problem occurs (memory overflow RAM), Swap will do the job of maintaining all normal operations even though the speed is significantly slower instead of stopping the whole system, making the data vulnerable because it has not been written to the hard disk.
III. When do we not need to use Swap?
In case your server has enough RAM or does not require Swap space usage or having Swap significantly reduces your system performance, then you should consider disabling the Swap partition.
IV. How to disable SWAP in Linux
To disable SWAP in Linux, we follow these 3 steps.
Step 1: SSH into your Linux system
To disable SWAP in Linux, we first need to SSH or access your VPS/server with root privileges. If you don’t know how to SSH into your VPS/Server, you can refer to the following tutorial:
After successfully SSH, we continue with step 2 to examine before disabling Swap in Linux.
Step 2: Investigate physical memory usage
Before actually disabling swap space, you need to first visualize the physical memory usage of the server and then determine the partition containing Swap, by running the commands below.
Here are the results of my VPS.
Find the used size of Swap, here mine is 23M (23 Megabytes). If the size used is 0B or close to 0 Bytes like mine, it is understandable that the Swap partition is not used much and can be safely disabled.
Next, we use the
blkid command, find the line TYPE=”swap” to identify the swap partition, as shown below:
Or alternatively, you can use the command
lsblk following to search and identify the [SWAP] partition as below:
So both results give Swap partition:
Now let’s go to step 3 to start disabling Swap in Linux.
Step 3: Execute the commands to disable Swap in Linux
After identifying the Swap partition in step 2, we execute one of the following two commands to disable Swap in Linux.
swapoff /dev/mapper/centos-swap hoặc swapoff -a
After running one of the two commands above, you can recheck with the
free -h command to check if the Swap partition has been disabled.
To permanently disable the Swap partition in Linux, open the
/etc/fstab file, search for the Swap line and disable the whole line by adding a # (hashtag) in front of the line.
Then save it and restart the Linux server with the
So we have finished disabling Swap in Linux.
With just 3 simple steps above, you can disable Swap in Linux. Remember to only disable Swap when absolutely necessary.
Hope this article will be of help to you. Wishing you success!
If you need assistance, you can contact support in the ways below:
- Hotline 247: 028 730 24768 (Ext 0)
- Ticket/Email: You use the email to register for the service and send it directly to: email@example.com .