LFCS 89 ๐ง Swap Space โ Concepts
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
| Form | Create | Advantage | Disadvantage |
|---|---|---|---|
| Partition | mkswap /dev/sdX | Raw block device, fastest | Fixed size |
| File | fallocate + mkswap | Flexible, no repartition | Filesystem overhead |
| LV | lvcreate + mkswap | Flexible, raw device | LVM setup required |
Swap Commands
| Command | Effect |
|---|---|
mkswap /dev/sdX | Format as swap |
swapon /dev/sdX | Activate swap |
swapoff /dev/sdX | Deactivate swap |
swapon --show | List active swap |
cat /proc/swaps | Show swap details |
free -h | Show memory and swap usage |
Swappiness Values
| Value | Behavior | Use Case |
|---|---|---|
| 0โ10 | Avoid swapping | Servers, latency-sensitive |
| 10โ30 | Balanced | Desktops |
| 60 | Default | General purpose |
| 100 | Aggressive | Rarely appropriate |
Size Guidelines
| RAM | Swap | Note |
|---|---|---|
| โค 2 GB | 2ร RAM | Traditional rule |
| 2โ8 GB | = RAM | Traditional rule |
| > 8 GB | 4โ8 GB | Or none for servers |
| Hibernation | โฅ RAM | Must 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
| Pitfall | Why It Happens | Fix |
|---|---|---|
swapon fails | Device not formatted with mkswap | Run mkswap first |
| Swap not active after reboot | No fstab entry | Add UUID=... none swap defaults 0 0 |
lvremove fails | Swap still active | swapoff first |
| System slow under memory pressure | Swappiness too high or disk too slow | Lower vm.swappiness, check disk |
| Hibernation fails | Swap smaller than RAM | Resize 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
| Item | Value |
|---|---|
| Purpose | Disk area for inactive memory pages |
| Forms | Partition, file, LV |
| Format | mkswap /dev/sdX |
| Activate | swapon /dev/sdX |
| Deactivate | swapoff /dev/sdX |
| Check | swapon --show, free -h |
| Persistent | /etc/fstab: UUID=... none swap defaults 0 0 |
| Swappiness | vm.swappiness, 0โ100, default 60 |
| Hibernation | Swap โฅ 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
mkswapand activated withswapon. The partition is the fastest; the file is the most flexible; the LV combines both . - The swap file requires restrictive permissions.
chmod 600 /swapfileprevents other users from reading the swapped memory contents . - The
fstabentry makes swap persistent. The format isUUID=xxxx none swap defaults 0 0for a partition or LV, or/swapfile none swap defaults 0 0for a file . vm.swappinesscontrols 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.
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!