Swap space on Linux servers acts as an extension of RAM, providing additional virtual memory when physical memory (RAM) is fully utilized. This helps prevent out-of-memory errors and keeps applications running smoothly. In this article, we’ll cover what swap space is, why it’s useful, and how to configure it on your Linux server.
What is Swap Space? #
Swap space is a dedicated area on your disk that functions as virtual memory, temporarily storing inactive pages from the main memory (RAM). When the system runs low on RAM, Linux uses swap space to free up memory by moving some processes from RAM to disk. While swap space is slower than RAM, it allows the system to handle memory overflows gracefully.
Why Use Swap Space? #
- Handle RAM Overloads: Swap space acts as a safety net when physical memory is insufficient, allowing the system to continue running by offloading some of the memory load to disk.
- Improve Stability: Without swap, memory-intensive applications may crash if RAM runs out, while swap space helps mitigate these situations.
- Enable Hibernation: Some systems require swap space to store the contents of RAM during hibernation.
Prerequisites #
- Linux Server Access: You need root or sudo access to the Linux server to configure swap.
- Free Disk Space: Ensure that your disk has enough free space to create a swap file or partition.
Step 1: Check for Existing Swap Space #
First, check if swap space is already enabled on your server. Use the following command:
sudo swapon --show
If there is no output, it means there is no active swap space. Alternatively, you can view memory usage, including swap, using:
free -h

Step 2: Creating a Swap File #
If no swap space is active or you need additional swap, creating a swap file is often the easiest method. Follow these steps:
a. Create a Swap File #
Determine the size of swap space you want (e.g., 1GB) and create a file for it. Adjust 2G
as needed:
sudo fallocate -l 1G /swapfile
If fallocate
is not available, use dd
:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
b. Set Correct Permissions #
Ensure that only the root user can read/write to the swap file:
sudo chmod 600 /swapfile
c. Configure the Swap File #
Set up the swap area on the file:
sudo mkswap /swapfile
d. Enable the Swap File #
Activate the swap file so the system can start using it:
sudo swapon /swapfile
e. Verify the Swap #
Confirm that the swap file is active:
sudo swapon --show

Step 3: Make the Swap Permanent #
To ensure that the swap file is used after a reboot, add it to /etc/fstab
:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 4: Configure Swap Usage (Optional) #
Linux systems use a parameter called swappiness to control how often swap is used. The default value is usually 60, meaning the system will start swapping when RAM usage reaches around 60%.
To view the current swappiness:
cat /proc/sys/vm/swappiness
To adjust the swappiness value to 10 (lower values mean the system uses swap less frequently):
sudo sysctl vm.swappiness=10
To make this change permanent, add the following line to /etc/sysctl.conf
:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Step 5: Adjusting Cache Pressure (Optional) #
The vfs_cache_pressure parameter controls how aggressively the system reclaims memory used for directory and inode caches. A lower value tells the system to retain more cache, which may improve performance.
To check the current setting:
cat /proc/sys/vm/vfs_cache_pressure
To reduce cache pressure, set the value to 50:
sudo sysctl vm.vfs_cache_pressure=50
Add this line to /etc/sysctl.conf
to make it persistent:
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
Step 6: Monitoring Swap Usage #
To monitor swap usage, use the following command:
free -m

You can also use top
or htop
commands to view swap usage in real-time.
Step 7: Disabling and Removing Swap (Optional) #
If you need to disable swap, you can turn it off temporarily with:
sudo swapoff /swapfile
To permanently remove the swap file, delete the entry from /etc/fstab
and remove the file:
sudo rm /swapfile
Author’s Final Word #
Setting up swap space on a Linux server can be crucial for handling memory overflows and improving system stability. While it’s not a replacement for physical RAM, swap space can keep your applications running smoothly in times of high memory usage. Adjusting parameters like swappiness and cache pressure also provides fine-grained control over how your server uses swap, helping optimize performance.