How To Increase Swap on Ubuntu 18.04
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.
free -m
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:
Command (m for help): n <== For new partition Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p <== Primary partition Partition number (1-4, default 1): 1 <== Select partition number, this is my 1st partition First sector (2048-10485759, default 2048): <== Just enter Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +1500M <== 1.5G
Created a new partition 1 of type ‘Linux’ and of size 1.5 GiB.
Command (m for help): t <== Change partition type from default Selected partition 1 Partition type (type L to list all types): 82 <== Set for swap Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.
Command (m for help): p <== List partitions Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xac02e15c
Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 3074047 3072000 1.5G 82 Linux swap / Solaris
Command (m for help): w <== Write changes The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
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.
free -m
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.
Conclusion
You have successfully increased swap memory on Ubuntu 18.04. Please share your feedback in the comments.