How To Increase Swap on Ubuntu 18.04

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,

  1. Using swap partition
  2. 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
Output
total used free shared buff/cache available Mem: 2508 1081 72 52 1354 1200 Swap: 2555 0 2555

The above Output confirms that my machine has 2.5GB of virtual memory.

Check which device/partition provides swap memory.

sudo swapon -s
Output
Filename Type Size Used Priority /dev/dm-2 partition 2617340 0 -1

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
Output
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors /dev/sda1 * 2048 499711 497664 243M 83 Linux /dev/sda2 501758 209713151 209211394 99.8G 5 Extended /dev/sda5 501760 209713151 209211392 99.8G 8e Linux LVM Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors

Let us create the partition with the id of 82 for swap memory.

sudo fdisk /dev/sdb

Follow the steps:

Output
Welcome to fdisk (util-linux 2.29.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.

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.

Output
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors /dev/sda1 * 2048 499711 497664 243M 83 Linux /dev/sda2 501758 209713151 209211394 99.8G 5 Extended /dev/sda5 501760 209713151 209211392 99.8G 8e Linux LVM Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors /dev/sdb1 2048 3074047 3072000 1.5G 82 Linux swap / Solaris

Increse Swap Memory

Make the swap partition using the following command.

sudo mkswap /dev/sdb1
Output
Setting up swapspace version 1, size = 1.5 GiB (1572859904 bytes) no label, UUID=2a86b13f-78c1-40b5-83aa-3f504445115c

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.

Output
Filename Type Size Used Priority /dev/dm-2 partition 2617340 0 -1 /dev/sdb1 partition 1535996 0 -2

Use the following command to see the memory available on the machine.

free -m

Now total swap is 4GB, as per our requirement.

Output
total used free shared buff/cache available Mem: 2508 647 487 5 1373 1669 Swap: 4055 0 4055

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.

Output
<file system>  <mount point>   <type>   <options>   <dump>   <pass> /dev/sdb1 none swap sw 0 0

Conclusion

You have successfully increased swap memory on Ubuntu 18.04. Please share your feedback in the comments.

comments powered by Disqus