Dynamic swap files are created only when needed

Swapspace is an excellent dynamic swap space manager

The disadvantage of large swap files is that they take up a lot of disk space even during those periods when programs have enough RAM and swap files are not used.

Related articles:

The Swapspace program solves this problem: swap files are created only when they are really needed, that is, when the operating system runs out of RAM. If the created Swap file runs out of space, another one is created. If it is not enough, then as many swap files are created as necessary. This prevents applications that need RAM from crashing. After the need for Swap files disappears, they are automatically deleted.

If you often need large swap files, then create a static large file without using Swapspace. Or, as a better option, increase the amount of RAM in your computer or server.

The Swapspace service can be used in conjunction with a fixed size swap file, in which case it will be taken into account when creating additional swap files.

Install Swapspace

To install swapspace on Debian, Kali Linux, Linux Mint, Ubuntu and their derivatives, run the command:

sudo apt install swapspace

To install swapspace on Arch Linux, Manjaro, BlackArch and their derivatives, install pikaur and then run the command:

pikaur -S swapspace

Setting up swapspace

The swapspace service does not need to be configured, it is easy enough to start!

Swapspace already has balanced settings for creating and deleting swap files. If desired, you can change the settings in the /etc/swapspace.conf file:

sudo gedit /etc/swapspace.conf

You can change the time after which swap files will be deleted after they are freed, you can set the maximum size of swap files created, you can change the location of swap files.

By the way, you can check the available disk space with the following command:

df -h /

Keep in mind that the swap file should only be readable by the root user, otherwise it would be a serious security hole.

As already mentioned, there is no need to configure anything, you can proceed to start the service.

Managing the swapspace service

Starting the swapspace service:

sudo systemctl start swapspace.service

Checking the status of the swapspace service:

systemctl status swapspace.service

Adding the swapspace service to autoload:

sudo systemctl enable swapspace.service

To stop and remove the swapspace service from startup, use the following commands:

sudo systemctl stop swapspace.service
sudo systemctl disable swapspace.service

Checking if swapspace works

To fill all available RAM, use the following command:

stress-ng --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 1.1;}' < /proc/meminfo)k --vm-keep -m 1

See also: How to check Swap file usage in Linux

As you can see, the swap file is being used, even though there was no swap file on the system before swapspace was started.

Check swap file usage with swapon:

swapon --show

It turns out that three swap files have been created, one of which is completely filled, the other is partially filled, and the third, apparently, has been prepared in advance:

NAME                 TYPE   SIZE   USED PRIO
/var/lib/swapspace/1 file 809,6M 808,6M   -2
/var/lib/swapspace/2 file 665,1M  90,7M   -3
/var/lib/swapspace/3 file 823,6M     0B   -4

Stop the stress test:

Some time after the end of the stress test (the specific time can be configured), the swap files are deleted automatically:

Leave Your Observation

Your email address will not be published.