Swap filesystem is one type of file system (id=82) in Linux and is used as virtual memory by the system when the system goes out of physical memory. In windows operating it is called pagefile.sys. Swap can be created during installation of the operating system or after that as well (when you wish to increase).
We can create the swap filesystem in two methods,
- Using swap partition
- Using a swap file
Here, we will the first method which is recommended for increasing the swap memory on Ubuntu 18.04.
Run all of the commands as the root user or privileged user using sudo.
Check Current Swap Memory
First, Let us check the available swap space on the system. Use free command.
The above Output confirms that my machine has 2.5GB of virtual memory.
Check which device/partition provides swap memory.
sudo swapon -s
Create Swap Partition
My system uses one of LVM partition (dm – Device Mapper) for swap memory.
For this demo, we will increase the swap form 2.5 GB to 4 GB using the partition.
My existing HDD on the system doesn’t have any space left for creating the new partition. So, I will use another HDD for a demonstration. However, you can use the same HDD for the new swap partition, if your HDD has enough space.
I have one extra HDD (/dev/sdb), and we are going create the new partition with the size of 1.5 GB for a swap and assigning the FS id of 82.
sudo fdisk -l | grep -i sd
Let us create the partition with the id of 82 for swap memory.
sudo fdisk /dev/sdb
Follow the steps:
Check whether you now have the new swap partition using fdisk -l | grep -i sd command.
Increse Swap Memory
Make the swap partition using the following command.
sudo mkswap /dev/sdb1
Enable the new swap using the following command.
sudo swapon /dev/sdb1
Verify the swap memory available on the machine.
sudo swapon -s
You can see that /dev/sdb1 has been activated and serves as swap memory.
Use the following command to see the memory available on the machine.
Now total swap is 4GB, as per our requirement.
If you would like to disable the newly added swap disk, you can use the following command.
sudo swapoff /dev/sdb1
In the normal case, if you restart the server, the swap memory will not get activated automatically. So you would need to put an entry in the /etc/fstab file.
sudo nano /etc/fstab
Make an entry like below.
You have successfully increased swap memory on Ubuntu 18.04. Please share your feedback in the comments.