In this guide, we will demonstrate on how to change the swap size in Ubuntu 22.04.
To perform the steps in this guide, you need the following components:
- A properly-configured Ubuntu system. For testing, you can install Ubuntu on a virtual machine using VirtualBox.
- Access to a non-root user who can execute the sudo commands.
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”.
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:
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:
- 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:
Now, we need to format the file as swap using the following command:
Finally, we can add the file to the swap pool.
If the action is successful, the new swap file should appear on the list of swap spaces.
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:
Checking the Free Swap Space
The following command prints both memory and swap usage:
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:
Check the list of active swap spaces to confirm the change.
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:
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:
Now, rerun the dd command to increase the size of the file:
Here, we added 2GB more space to the swap file. Next, we reformat the file as swap using the following command:
Finally, we can enable swapping to it:
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).
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.