Loading...
X

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


Leave Your Observation

Your email address will not be published.