How to add swap memory to your linux server

3 min read Original article ↗

You’ve launched your application on a new server, but it keeps crashing unexpectedly. Your server has limited physical memory (RAM), and a program called the Out-Of-Memory (OOM) Killer is likely the cause. The Linux kernel’s OOM Killer is a process that activates when the system is critically low on memory; its job is to sacrifice one or more processes to free up memory for the rest of the system to survive. While this prevents a full system freeze, it means your critical application might be the one that gets terminated.

While the long-term solution may be to optimize your application for memory leaks or upgrade your server’s RAM, these options take time and money. This is the fastest way to give your server a “safety net” when it runs low on physical RAM. Swap uses a file on your disk as a form of virtual memory. While slower than real RAM, it’s much better than having your application crash. This guide will walk you through creating and enabling a swap file on your Linux server to improve its stability.

Here’s how to create a 2GB swap file:

Workflow:

  1. First you check the disk space and memory of the server

  2. Then you create the swap file

  3. Set the correct permissions for the file

  4. Setup the file as swap space

  5. Enable the swap file

  6. Make the change permanent

Commands:

# see your disk space
df -h
# check your memory usage
free -h
# check if you have any existing swap
sudo swapon --show

# Create the swap file, It will create a swap file at root (/)
sudo fallocate -l 2G /swapfile

# Set the correct permissions, adds read and write permission for root
sudo chmod 600 /swapfile

# Format the file as swap space
sudo mkswap /swapfile

# Enable the swap file
sudo swapon /swapfile

# Make the change permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# check to see if swap is working
free -h

You can use df -h to see how much free disk space you have after running the app to determine how much you should give to swap space.

If your application’s memory usage is legitimate and necessary for its function, your server instance may simply be too small. Consider upgrading to more rams.

In the meantime inspect your application to see if it has any memory leak issue.

Swappiness is a Linux kernel parameter that controls how aggressively the system uses swap space. It is represented by a value between 0 and 100.

  • A low value (e.g., 10) means the kernel will try to avoid swapping as much as possible, only using it when absolutely necessary to prevent an OOM error. This is ideal for performance if you want your applications to stay in RAM.

  • A high value (e.g., 90) means the kernel will swap out inactive processes to disk more aggressively to free up RAM.

  • The default value on most systems is 60.

For a server, a lower value like 10 is often recommended to prioritize application performance.

# check your swapiness
cat /proc/sys/vm/swappiness

# set a new value for swapiness
sudo sysctl vm.swappiness=10

# make the change permanent by adding to sysctl.conf
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf