Categories: Ubuntu

How to Change the Swap Size in Ubuntu

In this guide, we will demonstrate on how to change the swap size in Ubuntu 22.04.

Prerequisites:

To perform the steps in this guide, you need the following components:

Swap File in Linux

The RAM of the machine is divided into chunks by the Linux kernel called “pages”. Whenever a page is copied to a preconfigured space on the secondary storage devices (hard disk or SSD in most cases), it’s called “swapping”. The preconfigured dedicated space is called the “swap space”.

Sponsored

The total amount of physical memory (RAM) and swap space is called the “virtual” memory.

Is Swapping Necessary?

There are a couple of reasons why swapping is needed:

  • When there’s a demand for more memory than what is physically available, swapping less important pages frees up more memory for the process that requires more memory.
  • A big portion of the pages used during the startup of a program may see very little usage. Swapping those pages can free up more memory for other apps.
  • If no swap space is assigned, if the RAM space is fully occupied, it can cause the system to crash, especially the systems with low RAM space.

However, swapping comes with its own set of downsides:

  • Compared to RAM, secondary storages (hard disks, SSDs, and such) are extremely slow. To put it into perspective, RAM access speeds are measured in nanoseconds whereas the disk access speeds are measured in milliseconds.
  • Because of the difference in read/write speeds, swapping is a very slow process. When a lot of swapping is happening, it eventually slows down the system.

Types of Swap Spaces

You will come across two types of swap spaces in Linux:

  • A dedicated swap partition – No other files can be stored there.
  • Swap files – These files can be anywhere within the filesystem.

Depending on the available RAM space, the size of the swap space can vary. Here are a couple of examples:

Swap File in Ubuntu

Ubuntu generally uses a dedicated swap partition for swapping. Oftentimes, this partition is created during the installation. However, we can create and tweak the swap files at will.

Listing the Swap Spaces

The following command shows all the swap spaces that are currently configured:

$ swapon

The list contains all the swap spaces, both partitions and swap files.

Creating Swap Files

A key advantage of swap files over swap partition is that the file size can easily be altered, thus changing the amount of swap space without touching the disk partitions. In this section, we create a new swap file and add it to the current swap pool.

First, create a blank file using the following command:

$ sudo dd if=/dev/zero of=/new_swap bs=1M count=2048

Here:

  • The file size is calculated as 1M X 2048 = 2G.
  • To create a file with a different size, change the value of the count argument accordingly.
  • The /dev/zero is a special block device in the Linux system that outputs zero bytes every time it’s read.
  • While we can use other tools like fallocate to create the file, in some situations, it can lead to problems. It’s discussed more in-depth in this AskUbuntu post.

Next, we need to set the correct file permissions using the following command:

$ sudo chmod 600 /new_swap

Now, we need to format the file as swap using the following command:

$ sudo mkswap /new_swap

Finally, we can add the file to the swap pool.

$ sudo swapon /new_swap

If the action is successful, the new swap file should appear on the list of swap spaces.

$ swapon

Note that this action is only temporary. Upon restart, the swap file will no longer be used. To make it a permanent change, we have to update the /etc/fstab table with the following entry:

$ /new_swap swap swap defaults 0 0

Checking the Free Swap Space

The following command prints both memory and swap usage:

$ sudo free -h

Sponsored

Deleting the Swap File

To delete a swap file, we first have to make sure that it’s not in use. The following command deactivates a swap file:

$ sudo swapoff -v /<swap_file>

Check the list of active swap spaces to confirm the change.

$ swapon

If the swap file is declared in /etc/fstab, you also have to remove the entry. Now, the swap file is safe to be deleted. Delete it using the following command:

$ sudo rm /<swap_file>

Changing the Swap Size

Depending on the swap space type (partition or file), the process of changing the swap size may vary.

Changing the Size of the Swap Partition

A partition can only be extended if there are unallocated spaces immediately after it. Otherwise, the only other resizing option is shrinking the partition. It also applies to the swap partition.

If you’re using the GNOME desktop, the “Disks” app can offer an insight into the situation.

Alternatively, we can use GParted to visualize it.

As you can see, the swap partition is directly next to the root partition in this system. This leaves no room for extending the swap partition.

However, shrinking and reformatting operations can be performed. Learn more about managing partitions using fdisk or GParted. The resize2fs command is also needed to resize the existing filesystem in accordance with the resized partition.

Changing the Size of the Swap File

To manipulate a swap file, we first need to remove it from the swap pool. Run the following command:

$ sudo swapoff /new_swap

Now, rerun the dd command to increase the size of the file:

$ sudo dd if=/dev/zero of=/new_swap bs=1G count=2 oflag=append conv=notrunc

Here, we added 2GB more space to the swap file. Next, we reformat the file as swap using the following command:

$ sudo mkswap /new_swap

Finally, we can enable swapping to it:

$ sudo swapon /new_swap

$ swapon

Note that in certain situations, trying to disable the swap file may result in an error like “swapoff failed: Cannot allocate memory”. In that case, we do the following:

  • Create a new swap file with bigger space.
  • Attach the bigger swap to the system.
  • Delete the older, smaller swap file.
  • Remove the older swap file entry from /etc/fstab (if applicable).

Conclusion

We discussed an in-depth demonstrated about managing the swap spaces in Ubuntu. We discussed the various types of swap spaces. We learned to resize the swap partitions and how to work with swap files (creating, deleting, and resizing).

Interested in mastering Ubuntu? Check out the Ubuntu sub-category which contains numerous guides on tweaking the Ubuntu system and using various tools.

Happy computing!

Ubuntu Server Admin

Recent Posts

Join Canonical in Brazil at Dell Technologies Forum São Paulo

Canonical is excited to be a part of the Dell Technologies Forum in São Paulo…

4 days ago

6 facts for CentOS users who are holding on

In 2020, it was announced that CentOS 7 would reach end of life (EoL) by…

4 days ago

What is Ubuntu used for?

The launch of Ubuntu in 2004 was a step-change for everyday users and developers everywhere.…

5 days ago

Ubuntu Weekly Newsletter Issue 862

Welcome to the Ubuntu Weekly Newsletter, Issue 862 for the week of October 13 –…

6 days ago

New Ubuntu Community Council 2024

Merlijn writes: I’m happy to announce the new 2024 Ubuntu Community Council! Heather Ellsworth (~hellsworth1)…

1 week ago