Operations 15 min read

Master Linux Swap: When, How Much, and How to Optimize Performance

This guide explains what Linux swap is, its advantages and drawbacks, how to size it for different memory configurations, and provides step‑by‑step commands for creating, managing, and tuning swap partitions or files, including swappiness and ZFS considerations.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Swap: When, How Much, and How to Optimize Performance

What is swap?

In Linux the physical memory is divided into pages. When RAM is low, inactive pages are written to a pre‑allocated area on disk called swap space, freeing RAM for active processes. The sum of RAM and swap constitutes the total virtual memory.

Advantages of swap

Large applications (e.g., LibreOffice) allocate a lot of memory only during start‑up; swap can off‑load these pages, leaving RAM for other tasks.

Hibernate on many distributions (e.g., Ubuntu) stores the contents of RAM in swap; the swap size should be at least as large as physical RAM for hibernation.

When RAM is insufficient, adding swap allows the system to keep running, albeit more slowly.

Drawbacks of swap

Swap resides on disk, which is orders of magnitude slower than RAM; heavy swap‑in/out degrades performance.

Severe swapping feels like the system has “died”. Adding physical RAM is the only real cure for chronic swapping.

Swap size recommendations

Ubuntu’s guidelines (RAM in GB):

If RAM < 1 GB and hibernation is not needed, set swap equal to RAM; for hibernation use twice RAM (capped at 2× RAM).

If RAM ≥ 1 GB and hibernation is not needed, set swap to sqrt(RAM). For hibernation use RAM + round(sqrt(RAM)), also capped at 2× RAM.

If the calculated swap is still insufficient, increase physical RAM instead of adding more swap.

Physical RAM (GB)   No Hibernate   Hibernate   Max
1                     1               2          2
2                     1               3          4
3                     2               5          6
4                     2               6          8
5                     2               7         10
6                     2               8         12
8                     3              11         16
12                    3              15         24
16                    4              20         32
24                    5              29         48
32                    6              38         64
64                    8              72        128
128                  11             139        256

Common swap operations

Check existing swap:

# swapon -s
Filename               Type   Size      Used   Priority
/data/.swapfile        file   10485756  6534248   -1
/data1/.swapfile       file   10485756  3246088   -2

Set swap priority: sudo swapon -p <priority> Enable a specific swap partition or file:

sudo swapon /dev/sda2          # enable a partition
sudo swapon -a                # enable all configured swap

Disable swap:

sudo swapoff /dev/sda2          # disable a partition
sudo swapoff -a                # disable all swap

Make swap permanent by adding entries to /etc/fstab:

/data1/.swapfile  none  swap  sw  0  0
/dev/zvol/dsk/rpool/swap2  swap  swap  default  0  0

Swap performance tips

Prefer a dedicated swap partition over a swap file; partitions provide contiguous disk space and generally better performance.

Place swap on a different physical disk from the system partition to avoid I/O contention.

If multiple disks are available, create equal‑size swap partitions on each and assign the same priority; the kernel will balance usage across them, roughly multiplying throughput by the number of disks.

Tuning the vm.swappiness parameter

The kernel parameter vm.swappiness (0‑100) controls how aggressively the system moves idle pages to swap. A value of 0 minimizes swapping; 100 maximizes it.

Typical defaults on Ubuntu are 60 for both desktop and server, but desktop systems with ample RAM often lower it to improve responsiveness.

# Set temporary value
sudo sysctl vm.swappiness=10
# Verify
cat /proc/sys/vm/swappiness
# Make permanent (add to /etc/sysctl.conf)
vm.swappiness=10
sudo /sbin/sysctl -p

Similarly, vm.vfs_cache_pressure can be tuned (e.g., set to 50) to control reclaim of inode and dentry caches.

Creating swap on ZFS

# 1. Create a ZFS volume of 2 GB
sudo zpool create -V 2G rpool/swap
# 2. Format it as swap
sudo mkswap -f rpool/swap
# 3. Activate it
swapon -a /dev/zvol/dsk/rpool/swap2
# 4. Persist in /etc/fstab
/dev/zvol/dsk/rpool/swap2  swap  swap  default  0  0

Creating swap on other filesystems

Swap partition

# Identify the new disk
sudo fdisk -l /dev/sdb
# Create swap partition (e.g., /dev/sdb1)
sudo mkswap /dev/sdb1
sudo swapon /dev/sdb1
# Verify
swapon -s
# Persist by adding to /etc/fstab
/dev/sdb1  swap  swap  default  0  0

Swap file

# Allocate files
sudo fallocate -l 512M /data/512M.swap
sudo fallocate -l 20G /data/20G.swap
# Secure permissions
sudo chmod 600 /data/512M.swap /data/20G.swap
# Format as swap
sudo mkswap /data/512M.swap
sudo mkswap /data/20G.swap
# Enable
sudo swapon /data/512M.swap
sudo swapon /data/20G.swap
# Verify
swapon -s
# Persist in /etc/fstab
/data/512M.swap  none  swap  sw  0  0
/data/20G.swap   none  swap  sw  0  0

To remove swap, disable it with swapoff and delete the corresponding entries from /etc/fstab.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory Managementperformance tuningSystem AdministrationSwapswappiness
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.