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.

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

However, RAM has its volatile space limit, if your system has 4GB of memory, it may happen many times, your RAM space is fully allocated when we run multiple 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 part of the hard disk dedicated to it is called “Swap” memory. If the data is needed again, it is reloaded into RAM and other data that is not needed at the moment is written to swap. In such a case, we also say: that “the system permutes”.

If a system uses swap extensively, the system becomes much slower and feels “harsh” due to frequent disk accesses. However, if you have no swap and the RAM is full, the Linux kernel terminates programs at its discretion to free up RAM. This usually results in data loss. To avoid exactly that, Ubuntu (like all other distros) creates a default swap area during installation. Newer versions of Ubuntu use a swap file whereas in older versions of Ubuntu a swap partition is used instead of the swap file. This usually has no drawbacks, 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 generally specified. Depending on system usage, approximately 1GB of swap memory or 1GB+ RAM capacity for disk suspend is sufficient. If the swap memory (and RAM) runs out, the system may crash and any data not in sync with the hard drive may be lost, so a generous swap area can be helpful. For example, if your system has 4 GB of RAM, 8 GB of paging file is sufficient if your hard drive has free space to allocate that amount.

1. Check current swap space

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

sudo swapon -s

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

2. Disable Exchange

To increase the Swap file size, start by disabling its use on your Ubuntu 22.04. Otherwise, the system won’t let us change its size. Also, 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 use.

3. Create Swap file on Ubuntu 22.04 to increase size

Next, create a new Swap file with the amount of space you want to allocate to it using the given command. When using the given command, keep one thing in mind: 8G means – 8GB of space that you are about to allocate to Swap. Therefore, you can increase or decrease according to your needs.

sudo fallocate -l 8G /swapfile

4. Change file permission

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

sudo chmod 600 /swapfile

5. Mark the SWAP space and activate it

Make the created file format readable as Swap by the system and also enable it to be used by our Ubuntu 22.04 or whatever version you are using.

sudo mkswap /swapfile
sudo swapon /swapfile

Create Swap File on Ubuntu 22.04

After activation, you can check whether Swap space is added to your system or not:

sudo swapon -s

Where

sudo swapon --show

Where

free -h

Mark the SWAP space and activate it

6. Set Ubuntu 22.04 SWAP file as permanent

If you have followed the steps above, you have already added swap memory to your system. And your system will also start using it immediately. However, upon reboot, it will be disabled. To avoid this and make the Swap Space file permanently added to your Ubuntu system, change /etc/fstab the file and add your Swap file information. Here is the command for that:

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

7. Define the use of the exchange or the exchange

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

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

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

sysctl vm.swappiness

The result might look like this:

vm.swappiness = 60

To change the Swappiness immediately, for example 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 into the system /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

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

vm.swappiness=25

Save the file using CTRL+Opress the Walk in key, then quit the file editor using CTRL+X.

Now reload it sysctl config file

sudo sysctl -p

Other Items:

Customize Ubuntu 22.04 Jammy JellyFish Dock Panel
List of commands to get Linux system information using terminal
Command to change time zone in Ubuntu 20.04 server
Krita FAQ: open source digital painting and drawing tool


Source link

Steven L. Nielsen