Do you need more memory? Activate swapping, or swap, on linux

To function properly, a computer system needs to have RAM memory . And the more gigs, the better. This memory is used to load all the programs and resources necessary for the PC to function properly. For starters, 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 space on the hard disk that, if necessary, is used as RAM memory. In this way, if our computer is running out of free RAM, 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.

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.

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 a while, 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 depend 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 that our computer has, and what we use it for. For example, if we have 4 GB of RAM, we should have some exchange gigs 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. It is from that threshold that 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 way we 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, of 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.

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 be able 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 verify that we are indeed using a file swap instead of a partition.

SWAP partition vs swapfile, which is better?

As we have seen, Linux has two types of exchange. On the one hand, we have the possibility of dedicating a complete partition to swap, and on the other, create a file that is responsible for storing the memory swap information, as Windows does.

Both techniques work the same, and neither has been shown to perform better or worse. However, the original partition method is far more cumbersome. We must always have a partition on the hard disk of a few gigabytes to use it as exchange memory. And in case we need to make it bigger, we will be much more limited, since if we have a contiguous partition we will not be able to make it bigger.

The use of the swapfile is much more comfortable. We can create this file, give it the size we want, and delete it with a simple command. We do not depend on partitions or special mount points, so we can play with the exchange as least we can at any time.

If our Linux distro allows it, we prefer to use the swap file, swapfile.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button