Configure and Use Swap Space on a VPS

By Anurag Singh

Updated on Oct 08, 2024

Configure and Use Swap Space on a VPS

In this tutorial, we'll learn how to configure and use swap space on a VPS.

Introduction

On Virtual Private Servers (VPS) with limited memory, swap space plays a crucial role in improving performance. Swap space allows the system to use disk space as additional memory, preventing applications from crashing when physical RAM is fully utilized. In this tutorial, we’ll go through the step-by-step process of configuring and using swap space on a VPS to optimize system performance.

What is Swap Space?

Swap space is a dedicated portion of your disk storage that acts as an overflow for your system's RAM (Random Access Memory). When the system runs out of physical memory, it moves inactive memory pages to the swap space, freeing up RAM for other processes. While swap space is much slower than RAM (since it's on disk), it prevents the system from crashing due to memory shortages.

Why Configure Swap Space?

  • Low-memory VPS: For VPS instances with limited memory, such as 512 MB or 1 GB, swap space is essential to ensure smooth operations.
  • Prevent Out-of-Memory (OOM) Errors: Without swap space, once the system runs out of memory, it could start terminating processes, leading to crashes.
  • Improve Performance: Swap allows for a temporary solution when RAM is under pressure, keeping applications running even when they require more memory than what’s available physically.

Prerequisites

Configure and Use Swap Space on a VPS

Step 1: Check Current Swap Space (if any)

Before creating new swap space, check if swap is already configured on your VPS.

sudo swapon --show

If there is no output, it means your system doesn’t have swap space configured yet. You can also verify by checking the memory usage:

free -h

Look for the Swap row in the output.

Step 2: Create a Swap File

To add swap space, we can create a swap file on the system’s disk. In this tutorial, we will allocate 1 GB of swap space, but you can adjust the size based on your VPS's needs.

Create the swap file using fallocate:

sudo fallocate -l 1G /swapfile

This command creates a file named /swapfile with a size of 1 GB.

Verify the swap file size:

ls -lh /swapfile

You should see output similar to:

-rw-r--r-- 1 root root 1.0G Oct  8 10:00 /swapfile

Step 3: Secure the Swap File

For security reasons, we need to ensure that only the root user can read and write to the swap file. This prevents unauthorized users from accessing it.

sudo chmod 600 /swapfile

You can confirm the file permissions with:

ls -lh /swapfile

The output should show the following permissions:

-rw------- 1 root root 1.0G Oct  8 10:00 /swapfile

Step 4: Mark the File as Swap Space

Next, we’ll format the file to use it as swap space.

sudo mkswap /swapfile

You should see output confirming the swap space creation:

Setting up swapspace version 1, size = 1 GiB (1073741824 bytes)
no label, UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Step 5: Enable the Swap File

Now, activate the swap space with the following command:

sudo swapon /swapfile

You can verify that the swap is active by checking:

sudo swapon --show

Or by using:

free -h

In the Swap row, you should now see the size of the newly added swap.

Step 6: Make the Swap Space Permanent

By default, the swap file is only enabled for the current session. To ensure it remains active after a reboot, we need to add it to the /etc/fstab file.

Open the /etc/fstab file with your preferred text editor:

sudo nano /etc/fstab

Add the following line at the bottom of the file:

/swapfile none swap sw 0 0

Save and exit the file (CTRL + X, then Y, and press Enter).

Step 7: Adjust Swappiness (Optional)

The swappiness value controls how aggressively your system uses swap space. The value ranges from 0 to 100. A lower value means the system will prioritize using RAM over swap space, while a higher value means swap will be used more frequently.

To check the current swappiness value:

cat /proc/sys/vm/swappiness

By default, it’s usually set to 60. For VPS instances with limited memory, setting it to 10 or 20 is recommended to avoid overusing swap space, as disk I/O is slower than RAM.

To temporarily change the swappiness value:

sudo sysctl vm.swappiness=10

To make this change permanent, edit the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

Add or modify the following line:

vm.swappiness=10

Save and exit the file.

Step 8: Adjust Cache Pressure (Optional)

The vfs_cache_pressure setting controls how much the system prioritizes reclaiming memory from the disk cache. Lowering this value can improve performance by keeping cached data longer.

To check the current cache pressure:

cat /proc/sys/vm/vfs_cache_pressure

To temporarily set it to 50 (which is less aggressive):

sudo sysctl vm.vfs_cache_pressure=50

To make this change permanent, edit the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

Add or modify the following line:

vm.vfs_cache_pressure=50

Step 9: Monitor Swap Usage

You can monitor swap usage with the free or top commands. Another option is to use the vmstat command to get a real-time view of memory and swap activity.

vmstat 2

This will update the memory and swap statistics every 2 seconds.

Conclusion

We have seen how to configure and use swap space on a VPS. By configuring swap space on your VPS, you can extend the system’s memory and improve performance, especially under heavy load or when dealing with memory-intensive applications. While swap space is slower than physical RAM, it serves as a useful fallback, ensuring your server remains stable even when RAM is fully utilized.