Fundamentals 16 min read

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

Linux swap extends virtual memory by moving infrequently used pages to disk, and this guide explains what swap is, its pros and cons, sizing recommendations for various RAM amounts, how to create and manage swap partitions or files, and performance tuning tips including swappiness and I/O optimization.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Swap: When, How Much, and How to Optimize It

Table of Contents

Introduction

Linux divides physical memory into pages. Swap is the process of copying memory pages to a pre‑allocated disk area (swap space) to free physical memory. The total size of physical memory plus swap space equals the total virtual memory available.

1. What is Swap

Understanding the role of swap in the Linux operating system!

Swap space is a region on disk that can be a partition, a file, or a combination of both. When physical memory is tight, Linux moves rarely accessed data to swap, freeing memory for other processes. When the data is needed again, it is swapped back into memory (swap‑out and swap‑in).

2. Advantages and Disadvantages of Swap

Use swap partitions flexibly at the right time!

Advantages

For large applications (e.g., LibreOffice) that consume a lot of memory during startup but use little thereafter, swap can store the unused pages, freeing physical memory for the system. Many distributions (e.g., Ubuntu) rely on swap for hibernation; the system saves memory contents to the swap partition during suspend and restores them on resume. When RAM is limited but memory‑intensive programs are needed, configuring sufficient swap allows them to run, albeit more slowly.

Disadvantages

Swap resides on disk, which is orders of magnitude slower than RAM. Frequent swap reads/writes degrade performance, especially when memory is heavily used. In such cases, adding physical RAM is the only effective solution.

3. Swap Size Recommendations

Guidelines for choosing an appropriate swap size for everyday use.

Based on Ubuntu's recommendations:

When physical memory < 1 GB and hibernation is not needed, set swap size equal to RAM; if hibernation is needed, set swap to twice the RAM (but not exceeding twice the RAM).

When physical memory ≥ 1 GB and hibernation is not needed, set swap to sqrt(RAM); if hibernation is needed, set swap to RAM + round(sqrt(RAM)) (still not exceeding twice the RAM).

If twice the RAM is insufficient, increase physical memory rather than swap.

Detailed recommendations:

Physical Memory (MB)   No Hibernation   Hibernation   Max
  256                  256              512           512
  512                  512              1024          1024
  1024                 1024             2048          2048
Physical Memory (GB)   No Hibernation   Hibernation   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

4. Common Swap Operations

Typical commands for adding and removing swap.

Linux supports two types of swap: swap partitions and swap files.

Swap partition

Since there is no filesystem on a swap partition, the kernel accesses a contiguous disk area directly, offering higher efficiency. However, resizing or expanding a swap partition after installation is inconvenient.

Swap file

A swap file resides within a filesystem, so its performance may be affected by the filesystem. Modern kernels (≥ 2.6) can access the underlying disk blocks directly, reducing overhead. Swap files are flexible and can be added or removed at any time.

1. View current swap configuration

# Filename: Type (partition or file) Size (KB) Used Priority
swapon -s
Filename               Type   Size      Used   Priority
/data/.swapfile        file   10485756  6534248 -1
/data1/.swapfile       file   10485756  3246088 -2

2. Make swap permanent

# Edit /etc/fstab
/data/.swapfile none swap sw 0 0
/data1/.swapfile2 none swap sw 0 0

3. Monitor swap in/out activity

# vmstat 2
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 3  1 9795592 2037192 282460 14034552   8    8    51    46   0    0 10  1 88  0  0
 3  0 9795592 2025832 282472 14044688   0    0 68279  270 5416 6425 35  6 54  5  0

4. Check overall memory usage

# free
              total        used        free      shared  buff/cache   available
Mem:       32887700    15661764    13404800      585080     3821136    15865296
Swap:      31457272    10461216    20996056

5. Swap Performance Optimization

Practical tips for improving swap performance.

Prefer swap partitions over swap files because partitions provide contiguous physical storage.

Place swap partitions on a different disk from the system partition to avoid competing for I/O bandwidth.

If multiple disks are available, create a swap partition on each and assign equal priority; the kernel will balance I/O across them, roughly multiplying performance by the number of disks.

However, if swap is accessed frequently, performance will always be limited by the speed gap between RAM and disk, so optimization has limited impact.

6. Configuring Swappiness

Reduce swap usage by tuning the swappiness parameter.

Swappiness (0‑100) controls how aggressively the kernel moves inactive pages to swap. A value of 0 tells the kernel to avoid swapping unless absolutely necessary; 100 forces swapping whenever possible. Ubuntu defaults to 60 for both desktop and server.

For desktops with ample RAM, setting swappiness to 10 can keep more data in memory, improving responsiveness. Servers, which prioritize overall throughput, usually keep the default 60.

1. Change swappiness temporarily

# sudo sysctl vm.swappiness=10
vm.swappiness = 10

2. Verify current value

# sudo cat /proc/sys/vm/swappiness
10

3. Make the change permanent

# Add or modify the line in /etc/sysctl.conf
vm.swappiness=10

4. Adjust vfs_cache_pressure (optional)

# sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Changes made via sysctl are lost after a reboot unless persisted in /etc/sysctl.conf as shown above.

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 ManagementlinuxSwap
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.