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:
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.
There are a couple of reasons why swapping is needed:
However, swapping comes with its own set of downsides:
You will come across two types of swap spaces in Linux:
Depending on the available RAM space, the size of the swap space can vary. Here are a couple of examples:
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.
The following command shows all the swap spaces that are currently configured:
The list contains all the swap spaces, both partitions and 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:
Here:
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:
The following command prints both memory and swap usage:
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:
Depending on the swap space type (partition or file), the process of changing the swap size may vary.
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.
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:
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!
At Canonical, the work of our teams is strongly embedded in the open source principles…
Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…
Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…
The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…
I don’t like my prompt, i want to change it. it has my username and…
Introduction: A Fragile Trust The Ruby ecosystem relies heavily on RubyGems.org as the central platform…