| |

LFCS 89 ๐Ÿง Swap Space โ€” Concepts

โฑ๏ธ Estimated read: 12 min | |

Swap space is the disk area that the kernel uses to store memory pages that it cannot keep in physical RAM. It is not a filesystem, it is not mounted, and it is not a backup. It is a temporary holding area for pages that the kernel has decided are not needed right now, so that the physical memory they occupied can be used for something else. The LFCS storage domain lists “configure and manage swap space” alongside partitioning and filesystem creation, because swap is the part of memory management that the administrator configures directly .

This chapter covers the concepts of swap: what it is, why the kernel uses it, the three forms it can take, and the swappiness parameter that controls how aggressively the kernel reaches for it. It assumes the storage concepts from the previous chaptersโ€”partitions, LVM, and filesystemsโ€”and establishes the mental model for the commands that create and manage swap.


Key point: Swap is not extra RAM. It is disk space that the kernel uses when physical memory is exhausted. Accessing a page from swap is orders of magnitude slower than accessing it from RAM, and a system that is swapping heavily is a system that needs more memory.


Why swap exists

The overcommit problem. The kernel promises more virtual memory to processes than physical RAM can hold. This is deliberate: most processes do not use all the memory they reserve, and refusing to overcommit would waste RAM. When the promises come due and physical memory is full, the kernel must either kill a process or move some pages to disk. Swap is the second option . Without swap, the out-of-memory killer is the only recourse.

The inactive page problem. A process may hold memory that it has not accessed in hours: a background daemon’s initialization data, a browser tab that has been idle, a cached library that is no longer needed. The kernel can move those pages to swap and reuse the RAM for file cache or for active processes. The pages are still there when the process needs them, and the cost is a page fault and a disk read . The system is faster overall because the RAM is being used for something more useful.

The hibernation requirement. Hibernation writes the entire contents of RAM to disk and powers off. The swap space must be large enough to hold the RAM contents, or hibernation fails. This is the one case where the swap size must be at least the size of physical memory .

The failure mode difference. A system with no swap and no free RAM is a system that will invoke the OOM killer. A system with swap and no free RAM is a system that becomes slow but survives. The trade-off is performance versus resilience, and the swappiness parameter is the dial between them.


a. The three forms of swap

Swap can be a dedicated partition, a swap file, or a logical volume. The kernel treats all three identically once they are activated; the difference is in how they are created and managed.

A swap partition is a partition on a disk that is formatted with mkswap and enabled with swapon. It is the traditional form, and it has the best performance because the kernel reads and writes the raw block device without a filesystem layer in between . The disadvantage is that the size is fixed at partition creation, and changing it requires repartitioning.

A swap file is a regular file on an existing filesystem that is formatted with mkswap and enabled with swapon. It is the most flexible form: it can be created, resized, or removed without touching the partition table. The Red Hat documentation for RHEL 10 shows the sequence: fallocate -l SIZE /swapfile, chmod 600 /swapfile, mkswap /swapfile, swapon /swapfile, and an fstab entry to make it persistent . The disadvantage is a small performance overhead from the filesystem layer, and the risk of fragmentation on some filesystems .

A swap logical volume is an LV in an LVM Volume Group that is formatted with mkswap. It combines the flexibility of a file with the performance of a partition, because the LV is a raw block device but can be resized with lvextend and lvreduce like any other LV. The Red Hat documentation includes the sequence for extending a swap LV: swapoff, lvresize, mkswap, swapon . This is the form that the LFCS exam is most likely to test, because it combines the LVM competencies from the previous chapters with the swap competency.


b. The size calculation and the swappiness parameter

The swap size depends on the workload and the amount of RAM. The traditional rule is that a system with less than 2 GB of RAM should have swap equal to twice the RAM; a system with 2โ€“8 GB should have swap equal to the RAM; a system with more than 8 GB should have 4โ€“8 GB of swap . The rule is a starting point, not a law. A database server with a large buffer pool may need no swap; a desktop with hibernation needs swap equal to RAM plus a margin.

The vm.swappiness parameter is a value from 0 to 100 that controls how aggressively the kernel moves pages from RAM to swap. The default is 60. A higher value makes the kernel swap more aggressively, which frees RAM for file cache but can slow down interactive processes. A lower value makes the kernel keep processes in RAM and reclaim file cache instead, which is better for latency-sensitive workloads . The common recommendations are 10โ€“20 for desktops and 1โ€“10 for servers with large memory .

The parameter is set in /etc/sysctl.conf or a file in /etc/sysctl.d/, and it is applied with sysctl -p. It can be changed at runtime with sysctl vm.swappiness=VALUE for testing, but the persistent configuration is the one that matters.

# Check the current swappiness.
cat /proc/sys/vm/swappiness

# Set it temporarily.
sudo sysctl vm.swappiness=10

# Set it persistently in /etc/sysctl.conf.
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

The swappiness value does not control whether swap is used at all; it controls the balance between reclaiming anonymous pages (which require swap) and reclaiming file-backed pages (which can be dropped without swap). A value of 0 tells the kernel to avoid swapping unless it is about to run out of memory, but it does not disable swap entirely .


c. Swap versus zram and zswap

The kernel has two compression mechanisms that change the swap picture. zram creates a compressed block device in RAM and uses it as swap. The pages are compressed and stored in memory, not on disk, so the access time is much faster than a disk-backed swap. zswap is a compressed cache in front of the disk swap: pages are compressed and stored in a memory pool first, and only written to disk if the pool is full . Both mechanisms reduce the disk I/O that swap would otherwise generate, but they consume CPU for compression and decompression.

For the LFCS exam, the relevant fact is that swap space must be configured and activated with mkswap and swapon. The zram and zswap mechanisms are optimizations that sit on top of that configuration, and they are not a substitute for understanding the basic swap partition or file.


Complete Example Session

# ============================================
# PART 1: CHECK THE CURRENT STATE
# ============================================
# Show active swap and memory usage.

free -h
swapon --show
cat /proc/swaps

# ============================================
# PART 2: THE SWAP PARTITION
# ============================================
# A dedicated partition is formatted with mkswap.

sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2

# ============================================
# PART 3: THE SWAP FILE
# ============================================
# A file on an existing filesystem.

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# ============================================
# PART 4: THE SWAP LV
# ============================================
# An LVM Logical Volume formatted as swap.

sudo lvcreate -n lv_swap -L 4G vg_data
sudo mkswap /dev/vg_data/lv_swap
sudo swapon /dev/vg_data/lv_swap

# ============================================
# PART 5: THE FSTAB ENTRY
# ============================================
# The swap must be activated at boot.

# For a partition or LV:
# UUID=xxxx none swap defaults 0 0

# For a file:
# /swapfile none swap defaults 0 0

# ============================================
# PART 6: THE SWAPPINESS PARAMETER
# ============================================
# The dial between RAM and swap.

cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10

# ============================================
# PART 7: THE EXTENSION SEQUENCE
# ============================================
# A swap LV can be resized.

sudo swapoff -v /dev/vg_data/lv_swap
sudo lvextend -L +2G /dev/vg_data/lv_swap
sudo mkswap /dev/vg_data/lv_swap
sudo swapon -v /dev/vg_data/lv_swap

# ============================================
# PART 8: THE REMOVAL SEQUENCE
# ============================================
# Disable before removing.

sudo swapoff -v /dev/vg_data/lv_swap
sudo lvremove /dev/vg_data/lv_swap

# ============================================
# PART 9: THE VERIFICATION
# ============================================
# Confirm the swap is active.

free -h
swapon --show

# ============================================
# PART 10: THE FULL PICTURE
# ============================================
# Swap is not RAM. It is disk space for inactive pages.
# Partition, file, or LV: all are formatted with mkswap.
# swappiness controls the balance.

The ten parts build the swap workflow from the forms to the parameters to the extension and removal. The pattern is consistent: format with mkswap, activate with swapon, disable with swapoff, and make it persistent in /etc/fstab.


Quick Reference

Swap Forms

FormCreateAdvantageDisadvantage
Partitionmkswap /dev/sdXRaw block device, fastestFixed size
Filefallocate + mkswapFlexible, no repartitionFilesystem overhead
LVlvcreate + mkswapFlexible, raw deviceLVM setup required

Swap Commands

CommandEffect
mkswap /dev/sdXFormat as swap
swapon /dev/sdXActivate swap
swapoff /dev/sdXDeactivate swap
swapon --showList active swap
cat /proc/swapsShow swap details
free -hShow memory and swap usage

Swappiness Values

ValueBehaviorUse Case
0โ€“10Avoid swappingServers, latency-sensitive
10โ€“30BalancedDesktops
60DefaultGeneral purpose
100AggressiveRarely appropriate

Size Guidelines

RAMSwapNote
โ‰ค 2 GB2ร— RAMTraditional rule
2โ€“8 GB= RAMTraditional rule
> 8 GB4โ€“8 GBOr none for servers
Hibernationโ‰ฅ RAMMust hold full RAM contents

Best Practices

โœ… Do This:

# Format before activating.
sudo mkswap /dev/sdX && sudo swapon /dev/sdX                          // โœ…
# Set restrictive permissions on a swap file.
sudo chmod 600 /swapfile                                              // โœ…
# Make swap persistent in fstab.
UUID=xxxx none swap defaults 0 0                                     // โœ…
# Lower swappiness for servers.
vm.swappiness = 10                                                    // โœ…

โŒ Don’t Do This:

# Don't activate an unformatted device.
sudo swapon /dev/sdX  # fails: no swap signature                     // โŒ
# Don't leave a swap file world-readable.
chmod 644 /swapfile                                                   // โŒ
# Don't assume swap is extra RAM.
# Accessing swap is disk I/O, orders of magnitude slower.            // โŒ
# Don't remove a swap LV without swapoff.
sudo lvremove /dev/vg/lv_swap  # fails: swap active                  // โŒ

Common Pitfalls

PitfallWhy It HappensFix
swapon failsDevice not formatted with mkswapRun mkswap first
Swap not active after rebootNo fstab entryAdd UUID=... none swap defaults 0 0
lvremove failsSwap still activeswapoff first
System slow under memory pressureSwappiness too high or disk too slowLower vm.swappiness, check disk
Hibernation failsSwap smaller than RAMResize swap to โ‰ฅ RAM

Real-World Examples

1. Create Swap Partition

sudo mkswap /dev/sdb2 && sudo swapon /dev/sdb2

2. Create Swap File

sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile

3. Create Swap LV

sudo lvcreate -n lv_swap -L 4G vg_data && sudo mkswap /dev/vg_data/lv_swap && sudo swapon /dev/vg_data/lv_swap

4. Show Active Swap

swapon --show

5. Check Swap Usage

free -h

6. Set Swappiness

sudo sysctl vm.swappiness=10

7. Extend Swap LV

sudo swapoff -v /dev/vg_data/lv_swap && sudo lvextend -L +2G /dev/vg_data/lv_swap && sudo mkswap /dev/vg_data/lv_swap && sudo swapon -v /dev/vg_data/lv_swap

8. Disable Swap

sudo swapoff /dev/sdb2

9. fstab Entry

UUID=xxxx none swap defaults 0 0

10. Check Swappiness

cat /proc/sys/vm/swappiness

Visual

The Swap Mechanism

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  PHYSICAL RAM                                           โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  Active pages    โ”‚  Inactive pages                โ”‚  โ”‚
โ”‚  โ”‚  (processes)     โ”‚  (not accessed recently)       โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                            โ”‚                            โ”‚
โ”‚                            โ–ผ  kernel reclaims           โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  SWAP SPACE (disk)                                โ”‚  โ”‚
โ”‚  โ”‚  Inactive pages written to disk                   โ”‚  โ”‚
โ”‚  โ”‚  RAM freed for active use                         โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                                         โ”‚
โ”‚  When the process accesses the swapped page:            โ”‚
โ”‚  page fault โ†’ read from swap โ†’ page back in RAM         โ”‚
โ”‚  Cost: disk I/O, orders of magnitude slower than RAM    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Three Forms

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  PARTITION              FILE                LV          โ”‚
โ”‚  /dev/sdb2              /swapfile           /dev/vg/lv  โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€              โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€           โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€   โ”‚
โ”‚  mkswap                 fallocate           lvcreate    โ”‚
โ”‚  swapon                 mkswap              mkswap      โ”‚
โ”‚                         swapon              swapon      โ”‚
โ”‚                                                         โ”‚
โ”‚  Fastest                Most flexible       Flexible +  โ”‚
โ”‚  Fixed size             Filesystem overhead raw device  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Swappiness

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  vm.swappiness = 0                                      โ”‚
โ”‚  โ†’ avoid swap unless about to OOM                       โ”‚
โ”‚                                                         โ”‚
โ”‚  vm.swappiness = 10                                     โ”‚
โ”‚  โ†’ prefer keeping processes in RAM                      โ”‚
โ”‚  โ†’ reclaim file cache first                             โ”‚
โ”‚                                                         โ”‚
โ”‚  vm.swappiness = 60 (default)                           โ”‚
โ”‚  โ†’ balanced                                             โ”‚
โ”‚                                                         โ”‚
โ”‚  vm.swappiness = 100                                    โ”‚
โ”‚  โ†’ aggressive swapping                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Extension Sequence

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  swapoff -v /dev/vg/lv_swap                             โ”‚
โ”‚         โ”‚                                               โ”‚
โ”‚         โ–ผ  swap disabled                                โ”‚
โ”‚  lvextend -L +2G /dev/vg/lv_swap                        โ”‚
โ”‚         โ”‚                                               โ”‚
โ”‚         โ–ผ  LV grown                                     โ”‚
โ”‚  mkswap /dev/vg/lv_swap                                 โ”‚
โ”‚         โ”‚                                               โ”‚
โ”‚         โ–ผ  new swap signature                           โ”‚
โ”‚  swapon -v /dev/vg/lv_swap                              โ”‚
โ”‚         โ”‚                                               โ”‚
โ”‚         โ–ผ  swap active at new size                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
PurposeDisk area for inactive memory pages
FormsPartition, file, LV
Formatmkswap /dev/sdX
Activateswapon /dev/sdX
Deactivateswapoff /dev/sdX
Checkswapon --show, free -h
Persistent/etc/fstab: UUID=... none swap defaults 0 0
Swappinessvm.swappiness, 0โ€“100, default 60
HibernationSwap โ‰ฅ RAM

Key takeaways:

  • Swap is disk space for memory pages, not extra RAM. Accessing a swapped page requires disk I/O, which is orders of magnitude slower than RAM access. A system that is swapping heavily needs more memory, not more swap.
  • The three forms are a partition, a file, and an LV. All are formatted with mkswap and activated with swapon. The partition is the fastest; the file is the most flexible; the LV combines both .
  • The swap file requires restrictive permissions. chmod 600 /swapfile prevents other users from reading the swapped memory contents .
  • The fstab entry makes swap persistent. The format is UUID=xxxx none swap defaults 0 0 for a partition or LV, or /swapfile none swap defaults 0 0 for a file .
  • vm.swappiness controls the balance. Lower values keep processes in RAM and reclaim file cache; higher values swap more aggressively. The default is 60, and servers often use 1โ€“10 .
  • Hibernation requires swap equal to RAM. The entire contents of physical memory must fit in the swap space, or hibernation fails .

Remember: Swap is the kernel’s pressure valve. When physical memory is full, it moves the least useful pages to disk and frees the RAM for active work. The administrator’s job is to provide the space, format it correctly, activate it, and make it persistent. The swappiness parameter is the dial that determines how eagerly the kernel reaches for that space. A well-configured swap keeps a system alive under memory pressure; an over-configured swap masks the need for more RAM; an under-configured swap lets the OOM killer decide.


Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!