Swap: How to Enable Swap Memory in Linux

To function properly, a computer system needs to have RAM . And the more gigs, the better. This memory is used to load all the programs and resources necessary for the PC to function properly. To start with, modern operating systems (Windows 10, Ubuntu, macOS) need around 2 GB minimum to work well. And the more programs we use, the more memory we need. However, what happens if we run out of memory while using Linux? Then a technique known as “Swap” is used.

It is known as “Swap” within Linux systems as a certain hard disk space that, if necessary, is used as RAM. In this way, if our computer is running out of free RAM memory, data is moved from it to the hard disk, its space is freed and, if it is needed again later, it is accessed from said hard disk.

How to Enable Swap Memory in Linux

In Windows, Swap is what we know as a “pagefile” or virtual memory . While in Windows it has always been a file (pagefile.sys), in Linux, until relatively recently, we had to have a dedicated partition (formatted and mounted as swap) of the capacity we wanted to use for this purpose. the most modern distributions already use a file similar to that of Windows for this task.

Linux Swap

Advantages and disadvantages of Swap

Like everything, this feature has its own advantages and disadvantages. Among the main advantages we can highlight that it is a “fast and cheap” solution to RAM memory problems . Especially when they are sporadic problems. Also, swapping is required to be able to use some Linux features, such as hibernating the computer. In addition, in this case, we will need to have a Swap of a few gigs more than the total RAM that we have in the PC.

Having 8 GB of Swap is not the same as having 8 GB of RAM. Swap is much slower than RAM , and sending and retrieving information from the hard drive takes time, so we will notice a significant loss of performance. If we ever resort to this there is no problem, but if we have little RAM it is better to physically expand it than to rely on Swap. Also, if you have an SSD, in the long run making use of this exchange can damage it due to the large number of write cycles it takes.

Also, Swap is less important to Linux than RAM . This means that it will always be in the background, and it is very likely that on some occasion some program, and even the entire operating system, will fail.

Should I use Swap?

The answer to this question is complicated. It depends on the hardware our computer has, and what we use it for. For example, if we have 4 GB of RAM, we should have some exchange gigabytes ready so that, if needed, they are available. The same happens if we are one of those who tend to hibernate the computer often.

If we use applications that consume huge amounts of memory, such as Blender, a 4K video editor or edit very large photos in GIMP, then it is also advisable to have this space available, although we may not need it.

However, if we have a computer with moderate RAM (16 GB, for example), and we neither use hibernation nor use the previous programs, then the Swap will not be necessary , since our Linux will never use all this memory.

Swappiness: choose when we want Linux to use Swap

By default, Ubuntu (and many Linux distros) have a default swappiness set to 60. This means that Swap is not used until 60% of RAM is used. From that threshold is when the swap memory begins to be used.

We can modify this value by editing the following file with an editor with root permissions:

cat /proc/sys/vm/swappiness

We can change the desired threshold by modifying the default value for the one we want. For example, we can put a value of “90”, if we only want to start using this when we have 90% of the RAM used. Even more. This will make better use of the physical memory of the computer, which, in turn, translates into better performance.

If we do not want to modify this value, we can also change the swap temporarily with the following instruction, although we must bear in mind that, after restarting, it will return to the default swappiness value.

sudo sysctl vm.swappiness=10

The optimal value that we must configure depends on each one. If we have enough RAM, the higher the better. But if we want to be careful to avoid running out of memory, the default value is not bad.

How to activate Swap in Linux

There are two different ways to activate the use of Swap, depending on the type we use (partition or file). We can check if our Linux has Swap, and of what type, executing the following command in a terminal:

sudo swapon --show

With it we will be able to see the name, or mount point, the type and the size.

In the case that we use the typical form of the partition, then the only thing we have to do when installing Ubuntu is to create a partition, the size we want (1.5 times the RAM is recommended) formatted as Linux-SWAP. In addition, we must also assign it the Swap mount point so that the operating system prepares said partition to use it as a swap.

Ubuntu partición Swap

If we do this during the installation of the operating system we will not have to do anything else. Otherwise, if we want to add it later, we must specify this mount point in the fstab of our distribution so that it starts automatically on startup.

In case you want to enable the use of Swap through a file , what we must do is execute the following commands:

Create the swap file (choosing the size changing 1G for the value we want to give it):

sudo fallocate -l 1G /swapfile

Next, we give it permissions so that only root can write to said file with:

sudo chmod 600 /swapfile

We give the file a structure to function as a swap file with:

sudo mkswap /swapfile

And finally, we activate it with:

sudo swapon /swapfile

For this file to load at the beginning of the distro by default we must add its instruction in the fstab. This statement should be like one more mount point, with the following:

/swapfile swap swap defaults 0 0

It’s ready. We restart Linux and we can see how the new Swap partition is working. If we want, we can use the “sudo swapon –show” command again to check that we are indeed using a file swap instead of a partition.