LightYear
/Docs
DocsComputeConfigure Swap Space on Linux

Configure Swap Space on Linux

Add a swap file to prevent out-of-memory errors on servers with limited RAM.

beginner
5 min read
LightYear Docs Team
Updated April 24, 2026
swapmemorylinuxperformance
Ready to get started?

Swap space acts as overflow memory when your server's RAM is fully utilised. While not a substitute for adequate RAM, a swap file prevents out-of-memory (OOM) crashes on servers with limited memory.

Check Current Swap

>_BASH
$swapon --show
$free -h

If no swap is configured, swapon --show returns no output.

Create a Swap File

Create a 2 GB swap file (adjust size as needed):

>_BASH
$fallocate -l 2G /swapfile
$chmod 600 /swapfile
$mkswap /swapfile
$swapon /swapfile

Verify swap is active:

>_BASH
$swapon --show
OUTPUT
NAME      TYPE  SIZE USED PRIO
/swapfile file    2G   0B   -2

Make Swap Permanent

Add the swap file to /etc/fstab so it persists across reboots:

>_BASH
$echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Tune Swappiness

The vm.swappiness kernel parameter controls how aggressively the kernel uses swap (0–100). A value of 10 is recommended for servers:

>_BASH
$sysctl vm.swappiness=10
$echo 'vm.swappiness=10' | tee -a /etc/sysctl.conf
RAMRecommended Swap
1 GB2 GB
2 GB2 GB
4 GB2 GB
8 GB4 GB
16 GB+4–8 GB

[!NOTE] NVMe SSD-backed servers have fast swap performance. However, excessive swap usage indicates your server needs more RAM — consider upgrading your plan if swap is consistently in use.

Was this article helpful?

Your cookie choices for this website

This site uses cookies and related technologies, as described in our privacy policy, for purposes that may include site operation, analytics, and enhanced user experience. You may choose to consent to our use of these technologies, or manage your own preferences. Cookie policy