Linux online course

Linux Swap Partitions and the swapon Command

Learn what Linux swap space is, when swapping occurs, how to check active swap with swapon, and how RAM, workload, and hibernation affect swap sizing.

Linux swap is disk-backed space that the kernel can use when physical memory, or RAM, is under pressure. Swap may be provided by a dedicated swap partition or by a swap file.

This lesson explains virtual memory, memory pages, swapping behavior, swap sizing, and the commands used to inspect active swap areas.

What Is Linux Swap?

RAM is fast physical memory used by the operating system and currently running programs. It is much faster than persistent storage, but it is limited in capacity.

Swap space is disk-backed capacity that Linux can use to store memory pages temporarily. A memory page is a fixed-size unit of memory managed by the kernel. When appropriate, Linux can move less-active pages from RAM to swap, leaving RAM available for more active work.

A swap partition is a dedicated disk partition assigned the Linux swap type. It is not mounted as a normal directory and does not contain ordinary user files. A swap file serves a similar purpose but is a regular file specially configured for swap use.

Swap, RAM, and Virtual Memory

Virtual memory is the memory-management model that gives processes an address space managed by the kernel. That address space can be backed by physical RAM and, when necessary, by disk-backed swap.

ConceptRolePerformance characteristic

RAM — Fast physical memory for active programs and the operating system — Much faster than storage, but limited in capacity.

Swap partition — Dedicated disk area for temporarily storing memory pages — Slower than RAM because it uses storage.

Swap file — A file configured to provide swap space — Has the same general purpose as a swap partition; management is often more flexible.

Virtual memory — The kernel's model for managing process memory across RAM and backing storage — Makes memory management flexible, but storage-backed pages are slower to access.

When Does Swapping Happen?

Linux tracks which memory pages are being used actively and which have not been used recently. Under memory pressure—when available RAM is limited compared with current demand—the kernel may evict less recently used pages from RAM.

Evicted pages may be written to swap if they need a disk-backed location. The freed RAM can then support active processes, filesystem caches, or other kernel work. Linux may begin using swap before RAM reaches exactly zero free bytes because memory management also considers available memory, reclaimable caches, and kernel policy.

Swapping is therefore related to the combined demands of running processes, the amount of installed RAM, and the kernel's page-eviction decisions. A small amount of swap use does not automatically indicate a fault.

Performance Effects of Swapping

Storage access is substantially slower than RAM access. If a process needs a page that has been moved to swap, Linux must retrieve it from storage before the process can continue using that page.

Occasional swap activity can be normal. However, sustained swapping—sometimes called thrashing—can make a computer noticeably slow or unresponsive. This occurs when the system repeatedly moves pages between RAM and storage instead of spending most of its time running useful application work.

Common causes include applications whose combined memory requirements exceed comfortable RAM capacity, a single memory-intensive process, or a workload that keeps requiring pages that were recently evicted.

Planning Swap Size

A historical rule of thumb is to plan swap at roughly one to two times installed RAM. Treat this only as a starting point, not a universal requirement.

FactorEffect on planningExample consideration

Installed RAM — More RAM may reduce routine reliance on swap — A workstation with ample RAM may not need a large swap area for ordinary use.

Workload memory demand — Memory-intensive applications can require more backing space — Virtual machines, large builds, databases, and media applications may increase demand.

Hibernation requirement — Swap must preserve the system's memory contents during suspend-to-disk — Capacity generally needs to be sufficient for the memory state being saved, with implementation-specific overhead.

Available disk capacity — A larger swap allocation consumes persistent storage — Small disks may require a more carefully sized allocation.

Storage performance — Faster storage can reduce, but not eliminate, the penalty of swapping — Storage is still much slower than RAM.

When planning during installation, start with installed RAM and the expected workload. Then decide whether hibernation is required and how much disk space can reasonably be dedicated to swap. Current Linux systems may use either a swap partition or a swap file.

Inspecting Active Swap with swapon

The swapon command can enable swap and display information about active swap areas. To list currently enabled areas, run:

swapon -s

A typical result contains a header followed by one line for each active swap partition or file. The exact formatting can vary by system.

FieldWhat it identifiesHow to interpret it

Filename or device — The partition device or swap-file path — Identifies where the swap area is located.

Type — Whether the area is a partition or file — Helps distinguish a dedicated partition from a swap file.

Size — Total configured swap capacity — Shows how much space the area provides.

Used — Swap capacity currently occupied — Zero means no pages are currently using that area; nonzero use is not automatically a problem.

Priority — The area's swap priority — Helps the kernel choose among multiple enabled swap areas.

The modern alternative is:

swapon --show

This command displays active swap areas in a selectable-column format and provides the same operational information in a form that is convenient to inspect.

You can also compare RAM and swap totals with:

free -h

Do not treat a nonzero swap value from free -h as proof of a problem. Investigate whether swap use is persistent and whether the system is slow or under memory pressure.

Example: Confirm a Swap Partition After Installation

  1. Run swapon -s.
  2. Identify the listed device, such as a partition device, and confirm that its type is a partition.
  3. Read the Size field to determine its total capacity.
  4. Read the Used field to see how much is currently occupied.
  5. Check the Priority field if multiple swap areas are listed.

If no swap device or file is listed, no active swap area is currently enabled. This does not prove that no swap storage exists: a swap area may have been created but not activated, or its persistent boot configuration may be missing or incorrect.

Multiple Swap Areas and Priorities

Linux can enable more than one swap partition, more than one swap file, or a combination of both. Consequently, swapon -s and swapon --show may list several active areas.

Swap priority is a value that influences which enabled swap area the kernel prefers. Priority can matter when several areas are available. The listing lets you see each area's priority, location, capacity, and current use.

Swap in Disk Partitioning

During operating system installation, a disk layout may include a regular Linux filesystem partition, other data partitions, and a dedicated swap partition. The swap partition is assigned the Linux swap type rather than being mounted at a directory such as /home or /var.

Dedicated partitions can be planned as part of the initial disk layout. Swap files are an alternative when flexible resizing or simpler post-installation management is more important than reserving a fixed partition.

Disk naming and partition identifiers are important when configuring swap to activate at boot. For broader partitioning context, review GPT partitions.

Troubleshooting Swap

No Active Swap Areas Are Listed

Likely causes:

  • No swap partition or swap file was created.
  • A swap area exists but has not been enabled.
  • The persistent boot configuration does not activate the intended area.

Confirm that swap storage exists, verify that it is enabled, and check the persistent definition used during boot.

The Computer Becomes Slow as Swap Use Rises

Likely causes:

  • Applications require more memory than available RAM can comfortably support.
  • A memory-intensive process is consuming a disproportionate amount of memory.
  • The system is repeatedly moving pages between RAM and slow storage.

Compare RAM and swap usage, identify high-memory workloads, and consider reducing application demand, adding RAM, or revising swap capacity and storage choices. Increasing swap may prevent an immediate out-of-memory failure, but it does not make disk-backed memory as fast as RAM.

Swap Disappears After Reboot

Likely causes:

  • The persistent swap entry is missing or incorrect.
  • The referenced partition identifier has changed or is invalid.
  • The swap device is unavailable during boot.

Verify the persistent swap definition and confirm the identity and availability of the intended partition or file. Basic command-line skills are useful when checking system configuration; see showing the full path of shell commands for related command-line practice.

Exam-Relevant Notes

  • Swap is disk-backed space; it is not the same as RAM or ordinary filesystem storage.
  • A memory page is a kernel-managed unit that can move between RAM and swap.
  • Linux can use swap before RAM is completely exhausted, depending on memory-management behavior.
  • Persistent heavy swapping usually indicates memory pressure or a workload that exceeds available RAM.
  • The one-to-two-times-RAM rule is historical guidance, not a universal sizing law.
  • Hibernation generally requires enough swap capacity to preserve memory contents.
  • swapon -s and swapon --show list active swap areas, including their location, type, size, usage, and priority.
  • Linux can use multiple swap partitions and swap files.