How to Configure Swap Space on Ubuntu Linux to Improve System Performance

Introduction

Swap space is a crucial component of Linux memory management. When a system’s physical Random Access Memory (RAM) becomes fully utilized, the Linux kernel relies on swap space—a designated area on the hard drive—to temporarily store inactive memory pages. This prevents applications from crashing due to Out Of Memory (OOM) errors. While traditionally configured as a dedicated partition during installation, modern Ubuntu systems often use swap files for greater flexibility. This guide demonstrates how to create and configure a swap file on Ubuntu to improve system stability.

Step 1: Check Existing Swap Space

Before creating a new swap file, verify if your system already has one configured. Running multiple swap spaces is possible but usually unnecessary.

Execute the following command in the terminal:

sudo swapon --show

If the output is empty, your system does not currently have swap enabled. You can also use the free -h command to view total RAM and swap utilization.

Step 2: Create the Swap File

We will create a 4 Gigabyte (4GB) swap file. The fallocate command is the fastest way to instantly allocate this space.

sudo fallocate -l 4G /swapfile

If fallocate is not available on your file system, you can use dd instead: sudo dd if=/dev/zero of=/swapfile bs=1M count=4096.

Step 3: Secure the Swap File

Because swap space can temporarily hold sensitive information (like passwords or encryption keys) that were in RAM, it is critical that regular users cannot read the file.

Change the permissions so only the root user can read and write to it:

sudo chmod 600 /swapfile

Step 4: Enable the Swap File

Next, you must format the file as swap space and activate it.

Format the file:

sudo mkswap /swapfile

Activate the swap space:

sudo swapon /swapfile

Run sudo swapon --show again. You should now see the /swapfile listed with a size of 4GB.

Step 5: Make the Swap File Permanent

Currently, the swap file is active, but it will not persist across a reboot. You must add it to the /etc/fstab file.

First, back up your fstab file:

sudo cp /etc/fstab /etc/fstab.bak

Then, append the swap file entry to the end of the file:

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

Step 6: Tune Swappiness (Optional)

The “swappiness” parameter dictates how aggressively the kernel moves data from RAM to swap. The value ranges from 0 to 100. A value of 0 tells the kernel to avoid swapping as much as possible, while 100 tells it to swap aggressively.

The default on Ubuntu is 60. For desktop environments, this is fine, but for production servers (like database servers), a lower value like 10 is often preferred to prioritize keeping data in fast RAM.

Check current swappiness:

cat /proc/sys/vm/swappiness

To change it temporarily to 10:

sudo sysctl vm.swappiness=10

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

sudo nano /etc/sysctl.conf

Add vm.swappiness=10 to the bottom of the file, save, and exit.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.