How to connect SWAP for virtual server: pros and cons of SWAP

How to connect SWAP for virtual server: pros and cons of SWAP

04.11.2022
Author: HostZealot Team
2 min.
918

SWAP is an important mechanism that provides reasonable management of virtual server memory by moving some data from RAM to SSD. The technology is relevant in cases where the system needs so much RAM that the server does not have physically. Activating swapping allows you to put the data that is least frequently used in a special partition on the drive.

Experienced Linux users know that a large number of pages used by programs during the startup phase are only needed during initialization and are no longer used during the entire work session. SWAP allows you to offload these unused pages from RAM and move them to storage space. Thus, swapping is an important tool for optimizing server systems.

Pros of SWAP

The main advantage of this technology is that it allows:

  1. To reduce server maintenance costs. That is, you can take a cheaper virtual private server plan with insufficient RAM, configure SWAP, and thus stabilize the server.
  2. To optimize server performance by freeing up memory pages.

On servers with little or no competition for memory, the benefit of this technology will be small – at most, you will be able to swap anonymous memory that applications and services rarely use. This will increase the cache hit ratio and free up precious RAM in general.

The technology will also be useful on VPS, where there are temporary bursts in terms of RAM consumption – resilience to these kinds of resource anomalies will be higher.

Cons of SWAP

The main disadvantage is that RAM is still faster. Even if you have a super modern NVMe drive on your server, RAM will be faster for information. So you should not abuse swapping, you need to be clear about what data is reasonable to put in this memory, and what is better left for RAM.

Also, SWAP can prevent your operating system from calling the Out-Of-Memory Killer, and activation of this process is sometimes necessary when you need to terminate an application that threatens a kernel crash. These situations are rare, especially on modern versions of Linux, but they happen.

how to connect swap for virtual server: pros and cons of swap

Creating and connecting a swap file SWAP

Suppose you have a VPS with 2 GB of RAM and no swap partition. In this case we first need to create a swap file:

sudo fallocate -l 2G /mnt/swapfile


We now have a swap file of 2 GB. If you have problems with fallocate, you can resort to the time-honored command dd:

sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048


Next, you need to assign read and write permissions for that file:

sudo chmod 600 /mnt/swapfile


And now that the file has been created and permissions have been set to administrator, we can tell the system the path to the swap file:

sudo mkswap /mnt/swapfile


Linux will now use this file to dump rarely used pages into it. Connect the file:

sudo swapon /mnt/swapfile


It remains to open the file /etc/fstab in edit mode and write there a line:

/mnt/swapfile  none  swap  sw  0 0


If this is not done, the swap file will only be active until the next server reboot, and then you will need to activate it again manually.

How do I check if there is a swap in the system?

There is a simple command for this:

swapon --show


When you activate it, you will see information about the swap file, its size and the number of megabytes involved.

How big to create a SWAP

The point is that the optimal paging file size is determined individually. It depends on these factors:

  • the amount of RAM on the VPS;
  • types of applications used, as well as their "voraciousness";
  • whether the server uses sleep mode.

If you have a virtual machine with less than 2 GB of RAM, the optimal size of the SWAP file is 1.5 or 2 times larger. On machines with 3-4 GB of RAM, in most cases a swap file of 10-15% larger than the amount of RAM is sufficient. If you have a VPS with 6 GB of RAM or more, a swap file of 1-2 GB will be enough.

Remember that if you have plenty of RAM, it is better not to activate swap file at all - if your system is not using 100% of available resources, you simply don't need swapping. You will only do harm, because moving data pages to the SWAP file will slow down the speed of data processing when accessing these pages.

Related Articles