How to add swap onto your GloboTech Linux-based instance

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Swapping is a valuable Linux process that can allow you to essentially add more memory to your Linux instance than it actually possesses by overwriting new new information into a preconfigured swap space. By default, our GloboTech Cloud instances come without swap enabled as swap can slow down the performance of a system. However, depending on your landscape you may still find the ability to artificially increase your memory a useful tool even at the cost of some performance.

Swapping will first and foremost allow you to circumvent a system needing more memory than is physically available. If a running process does not have enough memory, your Linux instance will switch to using the swap space and start swapping out less used RAM (random access memory) chunks known as pages and giving this memory to the process. In any Linux system, a significant number of memory pages that used by applications are in fact only used during their initialization. This means that even when running at full memory capacity, your Linux system will still have the capacity to utilize swap and free the memory from these unused pages.

However, as stated above, swapping does have a downside as it can be a slow process due to its direct reading and writing off of the main hard disk instead of to memory. While memory speeds are measured in nanoseconds, disk speed is instead measured in milliseconds. This means that when using swap, you may find that accessing the disk can be even tens of thousands of times slower than accessing the physical memory, and the more swapping that occurs, the slower your system will become. If you have an instance that a system is constantly swapping memory, and not only for a few particular use cases, then the only true solution will be increasing the RAM. If not, then swap is a reliable choice that can save you trouble in situations of tight memory availability. Without swap on your server, you can run into cases where your instance is simply out of memory and will crash, taking your applications temporarily offline. This makes swap ideal as a back-up plan in the case that your memory is overloaded, so that you can rest in peace knowing your system will be protected from memory-induced crashes.

If you find swap to be a good choice for your Linux-based instance, this guide will show you how you can add swap memory to your GloboTech Linux-based instance. Note that the information in this guide will pertain to the majority of Linux-based distributions such as CentOS, RHEL, Fedora, Ubuntu, and Debian.

Tutorial

Swap memory can be allotted to your instance in one of two ways. The first, more complicated method requires the creation of a new partition to be used for swapping. For this, you will oftentimes have to repartition your disk in order to create the empty partition required and this can create more difficulties for your landscape and can be rather risky.

The second method, creating a swap file, removes this requirement and is the preferred way to add swap memory with as little impact as possible on your existing infrastructure. The swap file acts as a swap partition with the advantage being that you will not need an actual empty partition or need to repartition the disk to use it. The size allotted to the file will determine the amount of swap memory that will be available on your server. This is the easiest method of adding swap memory as it is fail-proof, requires zero risky repartitioning, and can be done in only a few easy steps.

1. Ensure you have enough space

The swap file we create will require filesystem space. You will need to check your available space to make sure that you have enough by using the command:

df -h

The output should show something like the following example, where you can see that we have 8.8GB available on the disk:

Filesystem Size Used Avail Use% Mounted on
udev 491M 0 491M 0% /dev
tmpfs 100M 4.4M 95M 5% /run
/dev/sda1 9.7G 877M 8.8G 9% /
tmpfs 497M 0 497M 0% /dev/shm

2. Allocate the swap file

Having ensured that we have enough space on our system, we will now actually create the swap using the fallocate utility. You can also create your swap file using dd, but the fallocate tool is quicker and simpler to use and is the preferred method. Since our system only has 8.8GB of available disk space, we will create a swap file that is 1GB only. If you have more space on your system, you can create a larger swap file. As the root user, execute the following command replacing the size with your desired size to create a swap file in /swapfile. Use M to indicate Megabytes and G for Gigabytes when giving the size:

fallocate -l 1G /swapfile.swap

If you run into any issues using fallocate, you can always use dd as a fallback. Execute the following as root:

dd if=/dev/zero of=/swapfile.swap bs=1024 count=1048576

3. Set file permissions

With the file created, we will need to set restrictive permissions on it in order to prevent it from accidental alterations by system users, which would break the file.

chmod 600 /swapfile.swap

4. Prepare the swap file

Next, we will use the mkswap utility to configure the created file to be used as swap space. Execute the following command to turn the file into swap:

mkswap /swapfile.swap

5. Mount the swap file

The file is not yet available in your system until you mount it. Activate it using swapon:

swapon /swapfile.swap

To make the changes permanent, we will add the swap file to the fstab file. This will allow the new swap file to be mounted automatically on boot. Open the /etc/fstab file in any text editor such as nano shown below:

nano /etc/fstab

Add the following line to the file:

/swapfile.swap none swap defaults 0 0

6. Prevent system processes from abusing of the swap

Since swap memory is not as able to handle heavy usage as regular RAM without taking performance hits, we can takes steps to ensure that processes won’t abuse the swap space. This will help keep your system from slowing down, and can be accomplished by modifying the swappiness sysctl parameter. This parameter represents the preference/avoidance of the kernel for swap space, and can take any value from 0 to 100 with the default being 60. A lower swappiness value will improve the responsiveness on your system by reducing RAM swapping. Set the swappiness by opening the sysctl file in any text editor (for example, nano):

nano /etc/sysctl.d/99-sysctl.conf

Find the line for the vm.swappiness parameter, and modify it to look like the following with a swappiness value of 10:

vm.swappiness=10

Save and close the file when you are finished.

Conclusion

Congratulations! Having completed all steps in this guide, your Linux instance. Now that all steps are completed, your system has a permanent swap file created which will be auto mounted at every reboot. Enjoy the extra memory!