Tag: swapon

How to increase the swap partition in Linux Mint and Ubuntu. How to create a Swap file in Linux

In this article, using Linux Mint and Ubuntu as an example, we will learn how to view information on the swap partition, create a swap file, and also increase the space available for swap.

The swap file is a specific place on permanent storage (for example, on a hard disk), where data that does not fit in RAM is temporarily dumped. That is, it is an opportunity to increase the amount of RAM without buying RAM modules. But such a “virtual” RAM is slower.

You can view the current size of the swap partition with the command:

swapon --show

Or with the free command:

free -h

Or with top:

top

As you can see, there is a 2 GB swap file on the system, which, let's say, is not enough.

Disable and delete this paging file with the following commands:

sudo swapoff -a
sudo rm -f /swapfile

There must be enough space on the disk to fit the swap file of the size you specified.

To create an 8G swap file (change the value as you wish) run the command:

sudo fallocate -l 8G /swapfile

Then run the commands:

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Open System Monitor to make sure the system uses the swap file:

Or you can do it with the command:

swapon --show

That's all! You don't even need to reboot your system for the changes to take effect.

By the way, if you go again to change the size of the paging file (up or down – it doesn’t matter), then exactly follow the same instructions again, but specify a different size when creating the paging file.

How to delete swap file in Linux Mint and Ubuntu

To completely remove the paging file, run the commands:

sudo swapoff -a
sudo rm -f /swapfile

Then open the /etc/fstab file:

sudo gedit /etc/fstab

And remove the line from it:

/swapfile none swap defaults 0 0

Error “fallocate: fallocate failed: Text file busy”

If you encounter an error:

fallocate: fallocate failed: Text file busy

This means that you want to create a swap file, but a file with the same name already exists and is still in use.

To disable and remove it, run the commands:

sudo swapoff -a
sudo rm -f /swapfile

Managing Swap files on other Linuxes

To increase or create a swap file on other distributions, see the articles:

How to create a swap partition on another drive

You don't have to use the system disk for the swap, which can be low on space. You can create a partition on any drive in the system. For details, see the instruction “How to create a swap partition not on the system drive. How to move the swap partition to another disk”.

How to check swap file usage in Linux

Stress testing the system in a out of RAM scenario: How to check Swap file usage in Linux

Swap file and swap partition in Arch Linux (BlackArch): what to choose and how to add Swap

RAM, along with the central processor, is a determining factor in the performance of a computer. Moreover, the speed (frequency) of RAM is not so important, how important is its quantity – the amount of RAM.

The computer starts to work much slower if it does not have enough RAM. Moreover, if it has a swap partition, then it can copy some information from RAM to it and then read it – this reduces performance, but at least does not lead to a reset of running tasks and a computer freeze.

So, if the performance of your computer drops due to a lack of RAM, then I would recommend upgrading the hardware first, that is, buying more RAM sticks. But this is not always possible. Or, in addition to buying hardware RAM, you can add another swap partition.

What is Swap

Swap is a place in permanent storage (hard disk, SSD, USB storage) to which part of the RAM is temporarily dumped, which:

a) not currently used in calculations

b) does not fit in physical RAM.

What is the difference between swap partition and swap file

So, we have decided that the swap partition and swap file in Linux is called Swap.

Now let's look at the two available options: partition and swap file.

Swap partition

A swap partition is a dedicated disk space, a separate volume that is created when partitioning a disk during installation of an operating system.

If you want to create a swap partition after installing the operating system, for example, as follows: reduce the volume of one of the partitions, and create a Swap partition on the freed space, then most likely you will not be able to do this “hot”, that is, during operation this same operating system. You will have to boot from a USB stick or optical disc and perform the necessary operations from it.

Swap file

A swap file performs the same functions as a partition, but is more convenient to use.

The convenience of using the swap file is expressed as follows:

a) the swap file is created as a regular file, that is, there is no need to partition the disk

b) to connect the swap file, you do not need to restart the operating system

c) to change the size of the swap file, you do not need to restart the operating system

d) to disable and delete the swap file, you do not need to restart the computer

That is, the situation is quite real when you need a swap file for a short time – for example, for an hour. You can create it, connect it, perform a task that requires a significant amount of RAM, and disable and delete it after. This is especially true if you have, for example, a fast, but not large SSD drive – and you don't want to constantly keep the swap file, especially if it is not particularly needed most of the time.

Another option with a paging file is convenient on VPS hosting, when unnecessary server downtime due to maintenance is not needed, and the impossibility of physical access requires contacting the support service.

How to display information about the swap file in Linux

To check if the swap file is present, what size it is and how much it is being used, run the command:

swapon --show

Either the command:

free -h

How to create a swap file in Arch Linux

All subsequent commands must be run with root user privileges.

To create a sSwap file, run the fallocate command, along with which you need to specify the size of the swap file to be created. You can use abbreviations (M = megabytes, G = gigabytes). For example, to create a 16 GB swap file located at the /swapfile path, you need to run the command:

sudo fallocate -l 16G /swapfile

The fallocate command can cause problems with some file systems such as F2FS or XFS. A more reliable, though slower, alternative is to use the dd command (this example will create a 512 megabyte file):

dd if=/dev/zero of=/swapfile bs=1M count=512 status=progress

Set file permissions (readable swap is a huge local vulnerability):

sudo chmod 600 /swapfile

Format the file in swap:

sudo mkswap /swapfile

Activate the swap file:

sudo swapon /swapfile

Please note that already at this stage the swap file starts to be used – no system reboot is required!

To ensure that the swap file is used after each reboot, open the /etc/fstab file.

In GUI:

sudo gedit /etc/fstab

In the command line interface:

sudo vim /etc/fstab

And add this line to this file:

/swapfile none swap defaults 0 0

Note: The swap file must be specified by its location in the file system, not by its UUID or LABEL.

Removing the Swap file

To remove the swap file, you must first disable it, and then it can be deleted:

sudo swapoff -a
sudo rm -f /swapfile

Finally remove the relevant line from /etc/fstab.

How to check Swap File usage in Linux

Stress testing the system in a out of RAM Scenario: How to check Swap file usage in Linux

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:

How to check Swap file usage in Linux

The swap file will be used when the physical memory runs out. In order to check the operation of the Swap file, you need to artificially use up all the RAM. You don’t need to run many applications and open dozens of tabs in your web browser to do this, instead you can use stress testing tools like stress-ng.

See also:

Install stress-ng

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

sudo apt install stress-ng

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

pikaur -S stress-ng

How to fill 110% of the free memory

An example of a command that will consume 10% more RAM than system has free RAM:

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

After that, watch the memory consumption – you will see how the Swap file starts to be used.

On the command line, Swap file usage can be checked with the command:

swapon --show
Loading...
X