How to increase Swap Space in Ubuntu 22.04 LTS Jammy

Add the required Swap Space in Ubuntu 22.04 LTS Jammy JellyFish Linux, if you think the existing one is not enough on your Server or Desktop system. Users can also follow this tutorial for Ubuntu 20.04 LTS as well.

Linux (like almost all other operating systems) tries to keep the operating system components, currently required program data, etc. in RAM (main memory), since the access to the RAM is much faster than to data carriers such as hard disk, CD, or USB stick.

However, RAM has its limit of volatile space, if your system has 4GB of memory then it can happen many times your RAM space is allocated completely when we run many programs simultaneously. At this point, the Linux kernel begins to free up RAM by writing parts of the data stored in RAM to the hard disk, the portion of the hard disk that is dedicated to this is called “Swap” memory. If the data is needed again, it is loaded back into RAM and other data that is not needed at the moment is written to the swap. In such a case, it is also said: that “the system swaps”.

If a system uses the swap intensively, the system becomes significantly slower and feels “tough” due to the frequent disk accesses. However, if you do not have a swap and the RAM is running full, the Linux kernel terminates programs at its discretion to free RAM. This usually results in data loss. To prevent exactly this, Ubuntu (like all other distributions) creates a swap area by default during installation. Newer Ubuntu versions use a swap file whereas, in older Ubuntu versions, a swap partition is used instead of the swap file. This usually has no disadvantage, but the handling differs in some details.

Steps to add Swap area in Ubuntu 22.04 LTS Linux

The appropriate memory size depends on several factors and cannot be specified in general. Depending on the system usage, about 1 GB of swap memory or 1 GB + RAM capacity for suspend-to-disk is sufficient. If the swap memory (and RAM) runs out, the system may crash and all data not synchronized with the hard disk may be lost, which is why a generous swap area may be useful. For example, if your system has 4GB of RAM then 8GB of Swap file is good to go if your hard disk has spare space to allocate that much amount.

1. Check current Swap Space

Before moving forward to increase the Swap space on your Ubuntu 22.04, first, check what is the current size of the Swap file on your system. This not only gives you an idea about how much space you need to add more but also lets you know whether the Swap has been enabled on the system or not. For that use the given command:

sudo swapon -s

The Size column will show the amount of SWAP allocated on your system. For example, here we have 2GB.

Check current Swap Space

 

2. Turn off Swap

To increase the size of the Swap file, first, disable its usage on your Ubuntu 22.04. Otherwise, the system will not let us change its size. Moreover, even if you try to do so, an error will generate saying “fallocate: fallocate failed: Text file busy in Ubuntu 22.04?

sudo swapoff -a

Wait for some time, the system will terminate the process or move the data from Swap to RAM before disabling its usage.

 

3. Create a Swap file on Ubuntu 22.04 to increase the size

Next, create a new Swap file with the amount of space you want to allocate it using the given command. While using the given command keep one thing in mind that- 8G means – 8GB of space you are about to allocate for Swap. Hence, you can increase or decrease as per your requirements.

sudo fallocate -l 8G /swapfile

 

4. Change file permission

For security reasons change the file permission and give its full control only to the root user.

sudo chmod 600 /swapfile

 

5. Mark SWAP space and activate it

Make the created file format readable as Swap by the system and also activate the same to use by our Ubuntu 22.04 or the version you are using.

sudo mkswap /swapfile
sudo swapon /swapfile

Create a Swap file on Ubuntu 22.04

After activating you can check whether the Swap space is added to your system or not:

sudo swapon -s

or 

sudo swapon --show

or 

free -h

Mark SWAP space and activate it

 

6. Set Ubuntu 22.04 SWAP file as permanent

If you have followed the above-given steps, then you would have added a Swap memory to your system already. And your system will also start using it immediately. However, as you restart it will be deactivated. To avoid that and make the added Swap Space file permanently on your Ubuntu system edit /etc/fstab the file and add the information about your Swap file. Here is the command to follow for that:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

 

7. Set swap usage or swappiness

You can specify when the kernel moves data from memory to the swap partition or swap file. The Swappiness is specified with a value from 0 to 100, whereby 0 is only swapped out if there is no other way (memory full) and with 100 the memory is hardly used. The default value is 60, so the kernel is more prone to swapping. In general, no value can be recommended, as it depends on the respective system and user behavior.

If the Swappiness has been set very low and many programs run in parallel, it can easily happen that the memory is almost full and exactly then a program is started that puts a particularly heavy load on the main memory. Now the memory is exhausted and the system not only has to load the already cumbersome program, but also outsource it at the same time. The result is a fully utilized system.

The following command can be used to find out the current value of the Swappiness:

sysctl vm.swappiness

The result could look like this:

vm.swappiness = 60

To change the Swappiness immediately, e.g. lower it to 25, use this command:

sudo sysctl vm.swappiness=25

However, this change is forgotten by the system after a reboot unless the following is entered in the system file /etc/sysctl.conf:

sudo nano /etc/sysctl.conf

Add the following line at the end of the file. Change the value 25 to whatever you would like to set for Swap.

vm.swappiness=25

Save the file by using Ctrl+O, hit the Enter key, and then exit the file editor using Ctrl+X.

Now reload the sysctl configuration file

sudo sysctl -p

 

Other Articles:

Customize Ubuntu 22.04 Jammy JellyFish Dock Panel
List of Commands to get Linux system info using terminal
Command to change timezone in Ubuntu 20.04 server
Krita FAQ: Open-source digital painting and drawing tool

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.