How to Create Swap Space on Linux

There are pros and cons of using Swap Space on your Linux systems. If you are using a low-end VPS with limited amount of RAM such as 512MB and your application is using more and you are experiencing frequent crashes as a result, Redis Cache could be a good example here as it utilizes RAM to cache objects and Swap Space can be a remedy for this use case. However, Swap Space uses your hard disk space just like how Windows uses paging file as a supplement to RAM sizes which means the speed is much slower and can potentially cause delay or slowdown in your application as a result.

This can be a temporary fix for system crashes but it is not recommended to be a physical memory replacement. Here’s how you can create Swap Space on your Linux:

Step 1: Check if your system has any swap space already configured:

swapon --show

If the output is blank, that means there are no swap spaces configured.

Step 2: Create a swap file that is usually double the amount of your physical RAM, that means if you have 512MB of ram you would want to create 1GB of swap file or more which is up to you. We will use fallocate to create the swap file using the command below:

sudo fallocate -l 1G /swapfile

Replace the “1G” with your recommended swap file size. After running the above command you will need to run the command below to check if the swap file was created successfully:

ls -lh /swapfile

If the swap file was created successfully, you will see a result similar to this:

-rw-r--r-- 1 root root 1.0G Feb 17 12:21 /swapfile

Step 3: Now that the swap file is created, we want to make sure that it is only accessible to root with the commands below:

sudo chmod 600 /swapfile

Followed by the command below to mark as swap file:

sudo mkswap /swapfile

After the above command you would see a result similar to below which means the swap file was created sucessfully:

Setting up swapspace version 1, size = 1 GiB (1073741824 bytes)
no label, UUID=xxxxxx-xxxxxx-xxxxx-xxx-xxxxxxx

Step 4: Use the command below to tell the system to start using the new swap file:

sudo swapon /swapfile

Use the command below to check if the swap file is being recognized by the system properly:

sudo swapon --show

You should see a result similar to below:

NAME      TYPE  SIZE USED PRIO
/swapfile file  1G   0B   -2

Now run the command below to check the available disk space and swap file usage:

free -h

You should see something like this:

               total        used        free      shared  buff/cache   available
Mem:           512M        418M        80M         4M        10M        6M
Swap:          1.0G          0B        1.0G

Step 5: Now that the swap file is successfully created and in-use by the system, we need to make it available on every boot by running the following command:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Once it’s done, here are some recommended values for the swapfile and the commands to set them:

sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50

To make the above value available at boot, edit the sysctl.conf file by using nano:

sudo nano /etc/sysctl.conf

Paste the following at the bottom of the file and press CTRL+O to save and hit Enter, followed by CTRL+X to exit:

vm.swappiness=10
vm.vfs_cache_pressure=50

Step 6: If you want to verify that you have done everything correctly, reboot your server by running the below command:

sudo reboot

Once the server is booted up, run the command below to check if the swap space was created automatically on boot:

sudo swapon --show

If you see a swap file result then everything is working as it should.