swap
100%

The Filesystem · Lesson 8

swap

Learn how Linux uses, initializes, activates, sizes, and safely deactivates swap space.

Linux can move selected anonymous memory pages between RAM and swap-backed storage. This can retain inactive memory while freeing RAM for active workloads and filesystem cache, but storage is much slower than RAM. Swap is a capacity and memory-management tool, not a substitute for sufficient memory or an application memory limit.

How Swap Participates in Memory Management

The kernel can use swap before RAM is completely exhausted, depending on workload, memory pressure, cgroups, and tunables such as swappiness. File-backed clean pages can often be discarded and reread from their files, while anonymous pages need swap or must remain in RAM.

Heavy sustained swapping can cause severe latency or thrashing. Diagnose memory demand, working sets, pressure, and application limits rather than treating a larger swap area as a universal performance fix.

Which memory is a primary candidate for storage in swap?

Inspecting Active Swap

Use read-only commands first:

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

These show configured active swap and aggregate memory figures. A nonzero “used” value is not automatically a problem; correlate it with swap-in/out rates, memory pressure, latency, and workload behavior.

Which command lists active swap areas in a structured view?

Initializing and Activating a Swap Device

mkswap writes a swap signature and destroys the target's previous usable metadata. Practice only on a verified disposable target:

$ sudo mkswap /dev/VERIFIED-SWAP-TARGET
$ sudo swapon /dev/VERIFIED-SWAP-TARGET

Before mkswap, verify model, serial, size, persistent identity, existing signatures, mounts, RAID, LVM, encryption, and backups just as you would before mkfs. After activation, confirm the exact source with swapon --show.

For persistence, use the swap UUID in /etc/fstab with type and options appropriate to local policy:

UUID=VERIFIED-SWAP-UUID none swap sw 0 0

Which command activates an initialized swap area?

Swap Files and Other Back Ends

A swap file can provide flexible capacity without repartitioning, but creation requirements are filesystem-specific. The file must have restrictive permissions, suitable allocation with no unsupported holes or copy-on-write behavior, a swap signature, and activation. Follow the filesystem and distribution documentation instead of copying a generic fallocate recipe everywhere.

Compressed RAM devices such as zram can provide another swap tier with different CPU and capacity tradeoffs. Encrypted swap can protect pages at rest, while hibernation requires a resume configuration and sufficient suitable storage. These goals affect sizing and design.

There is no universal rule that swap must equal twice RAM. Size it from workload peaks, desired failure behavior, hibernation needs, storage latency and endurance, crash-dump design, and operational monitoring.

Which is the best basis for swap sizing?

Deactivating Swap Safely

Deactivate a specific verified area with:

$ sudo swapoff /dev/VERIFIED-SWAP-TARGET

The kernel must move its resident swapped pages elsewhere. If RAM and remaining swap cannot accommodate them, the operation can fail or create dangerous memory pressure. Stop or constrain workloads first, monitor memory, remove the persistent fstab entry only after verifying the correct target, and confirm deactivation with swapon --show before repurposing storage.

Why can swapoff fail or endanger a heavily loaded system?

Lesson complete

You finished swap

You can now treat swap as an explicit memory-management resource.

  • Relate swap primarily to anonymous memory under pressure.

  • Inspect active swap and workload behavior before changing capacity.

  • Initialize only a verified disposable target, then activate with swapon.

  • Size and secure swap according to workload and hibernation requirements.

  • Ensure relocation capacity before using swapoff.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to The Filesystem