Efficiently Resolve Linux Server Resource Bottlenecks with the Swap Technique

You have probably worked with a server that, due to limited resources while handling a heavy task, required you to use some tricks. I am currently learning how to manage a Linux server, and my goal with this article is to share what I have learned about using the swap partition/file as an emergency fix when the RAM is fully used.

Before opening the terminal and walking you through the procedure, I'd like to give a brief explanation of what swap memory is.

What is Swap Memory?

Swap memory is a reserved disk space used when the server runs out of RAM and reaches its full capacity. As simple as that.

I want to emphasize that this is an emergency method to address a specific issue and help the server and its services continue running. Swap memory is SLOW compared to RAM speeds.

Hands-On

For this procedure, I am going to use the following 2 resources:

  • A Linux server. In this example, I will connect to an Ubuntu Server in VirtualBox using SSH.

  • A terminal where you can run SSH. I will use Windows Terminal. If you don’t have it, you can download it from the Microsoft Store.

Once you have these ready, you can move on to the first step of the procedure.

  1. To establish an SSH connection to your server, use the following command:
    ssh yourusername@ipaddressofyourserver
    Then, enter your password.

  2. You're in! Now, let's check our available disk space usage with the lsblk command, which lists all block devices. We'll use the -f flag to include filesystem information in the output. Remember, you can always view the manual by typing "man" before the command.

    Now you can see a list with information about your drives. On my first and only hard drive, there are two partitions: sda1 and sda2. The sda1 partition contains GRUB, the boot loader my virtual machine uses, and the sda2 partition contains the entire server file system and programs. I have 21.6 gigabytes available, so I have plenty of space to create the swap file.

  3. In this step, we will preallocate disk space for our swap file. We'll use the fallocate command, which requires sudo privileges, along with the -l flag to specify the file size in bytes. We are going to use up to 2 gigabytes, so the command will look like this:

  1. To protect the file, we will set its permissions to 600 using the chmod command, so only the root user can read and write to the swap file.

  2. At this point, we have created the swap file and restricted who can modify it, but we haven't yet converted the swap file into an actual swap space or made it persistent. Thankfully, both tasks are really simple to do. We will use the mkswap command to set up our Linux swap area. This command also requires sudo privileges.

    Once our swap area is set, we will receive a prompt that provides information such as its size and ID.

  3. How can we make this swap file persistent? To do this, we need to configure it in the "fstab" file, which contains all the given guidelines to our system partitions. We can find the "fstab" file in the "etc" directory. Let's configure it using vim!
    Use sudo vim /etc/fstab. Once you enter this into the terminal, you will see something like this:

    I will help you understand this. We have a table with the following format: <filesystem> <mount point> <type> <options> <dump> <pass>. In our case, the filesystem is the swap file we created earlier, the mount point is swap, the type is swap, and for the "options," we will use defaults. I want to pause here to briefly explain what dump and pass are.

    Dump is a command that tries to back up a partition in case of a disaster. On the other hand, pass runs a command called fsck, which checks the file system if something goes wrong. As you might guess, neither of these tools is needed in a swap configuration because swap is volatile memory, that’s why we are going to use 0 (zero) in both options.

    To modify a text file using vim, press "i" (for input) on your keyboard. Once you've done this, enter the following information into the fstab file:

    Once done, press “Esc” button on your keyboard to exit input mode and then, press “:” and type “wq” to save and exit the vim editor.

  4. Now we need to inform our server that the swap file exists and activate it. You can check that the swap we created is not yet activated by using the free -h command. This command shows the amount of free and used memory on the system.

    To activate our swapfile, just type sudo swapon /swapfile in your terminal. Once you've done this, you can see the difference by using the free -h command.

    As you can see, 2 gigabytes of swap memory have now been added!

How do you remove the created swap memory?

  1. To remove it, the first step is to turn off the swap at a logical level. Open your terminal and type sudo swapoff /swapfile.

  2. Delete the previously added line from the fstab file. In vim, you can remove it by selecting the line and pressing "d" twice, then press “:” and type “wq”.

  3. Move to root directory and run the following command: sudo rm swapfile.

This guideline was created while I am learning how to manage Linux servers. I hope it's helpful to you and that you learned something today with me. For any questions or feedback, please email me at s3v4st0s@gmail.com.

0
Subscribe to my newsletter

Read articles from Sebastián Álvarez directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sebastián Álvarez
Sebastián Álvarez