Skip to content

Swap Space — Extending Memory in Linux

Swap Space is a reserved area on disk that Linux uses as virtual memory when physical RAM becomes full. It allows the operating system to move inactive memory pages from RAM to disk, helping prevent out-of-memory (OOM) situations and improving system stability. Although swap is significantly slower than RAM, it plays an important role in enterprise Linux systems, cloud environments, virtualization platforms, and Kubernetes worker nodes.


Learning Path

Linux Mastery → Module 9: Storage Management → Lesson 7

Difficulty: Beginner → Intermediate

Reading Time: 75 Minutes

Course Progress

Course: Linux Mastery

Module: Storage Management

Lesson: 7 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand swap space
  • Learn how virtual memory works
  • Create swap partitions and swap files
  • Enable and disable swap
  • Monitor swap usage
  • Configure swap persistence
  • Tune swap behavior
  • Apply swap best practices in production

Prerequisites

Complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Linux Command Line Essentials
  • Module 3 – Text Processing
  • Module 4 – File Management
  • Module 5 – Users and Groups
  • Module 6 – Process Management
  • Module 7 – Package Management
  • Module 8 – Networking
  • Module 9 Lessons 1–6

Why Learn Swap?

Imagine:

  • A database server temporarily runs out of RAM.
  • A Kubernetes node experiences memory pressure.
  • Multiple virtual machines share limited memory.
  • An application suddenly consumes excessive RAM.

Without swap:

RAM Full


Out Of Memory (OOM)


Processes Killed

With swap:

RAM Full


Inactive Pages Moved to Disk


System Continues Running

What is Swap?

Swap is disk space used as an extension of physical memory (RAM).

Memory hierarchy:

CPU Cache


RAM


Swap


Storage

RAM is much faster than swap, but swap provides additional virtual memory when needed.


How Swap Works

Applications


RAM


RAM Full


Inactive Pages


Swap Space

Frequently used data stays in RAM, while less frequently used pages may be moved to swap.


Types of Swap

Linux supports two types:

  • Swap Partition
  • Swap File

Swap Partition

A dedicated partition created specifically for swap.

Example:

/dev/sda2

Type: Linux Swap

Advantages:

  • Better performance
  • Common in enterprise deployments
  • Traditional approach

Swap File

A regular file used as swap.

Example:

/swapfile

Advantages:

  • Easy to create
  • Easy to resize
  • Common in cloud virtual machines

View Swap Information

Display swap usage.

swapon --show

Example:

NAME

TYPE

SIZE

USED

View Memory Usage

free -h

Example:

Mem

Swap

Create a Swap File

Create a 2 GB swap file.

sudo fallocate -l 2G /swapfile

If fallocate is unavailable:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Set Secure Permissions

sudo chmod 600 /swapfile

Only the root user should have access.


Format as Swap

sudo mkswap /swapfile

Example output:

Setting up swapspace...

Enable Swap

sudo swapon /swapfile

Verify:

swapon --show

Disable Swap

sudo swapoff /swapfile

Configure Persistent Swap

Add the following entry to:

/etc/fstab
/swapfile none swap sw 0 0

After reboot, the swap file will be enabled automatically.


Create a Swap Partition

Format the partition.

sudo mkswap /dev/sdb2

Enable it.

sudo swapon /dev/sdb2

View Swap UUID

blkid

Example:

UUID="abcd-1234"

TYPE="swap"

Persistent /etc/fstab entry:

UUID=abcd-1234

none

swap

sw

0

0

Swappiness

Linux uses the swappiness parameter to determine how aggressively it moves memory pages to swap.

View current value.

cat /proc/sys/vm/swappiness

Example:

60

Temporarily change it.

sudo sysctl vm.swappiness=20

Persist the setting.

Edit:

/etc/sysctl.conf

Add:

vm.swappiness=20

Typical values:

Value Behavior
0 Avoid swap unless absolutely necessary
10–20 Preferred for database servers
60 Linux default on many distributions
100 Use swap aggressively

Common Commands

View swap.

swapon --show

View memory.

free -h

Create swap.

mkswap /swapfile

Enable swap.

swapon /swapfile

Disable swap.

swapoff /swapfile

Real Production Examples

Check swap usage.

free -h

Enable swap.

sudo swapon /swapfile

Monitor memory.

vmstat 2

Adjust swappiness.

sudo sysctl vm.swappiness=10

Production Perspective

Swap is commonly used for:

  • Linux servers
  • Cloud virtual machines
  • Kubernetes worker nodes
  • Virtualization hosts
  • Development environments
  • Desktop systems
  • Memory-intensive workloads

Swap improves system stability but should not replace adequate physical RAM.


Hands-on Lab

Task 1

View current memory usage.

free -h

Task 2

Display active swap.

swapon --show

Task 3

Create a 1 GB swap file.

sudo fallocate -l 1G /swapfile

Task 4

Set permissions.

sudo chmod 600 /swapfile

Task 5

Format the swap file.

sudo mkswap /swapfile

Task 6

Enable swap.

sudo swapon /swapfile

Task 7

Verify swap.

free -h

swapon --show

Task 8

Disable swap.

sudo swapoff /swapfile

Command Deep Dive

Command Purpose Production Example
free -h View memory usage Capacity monitoring
swapon --show Display active swap Memory management
mkswap Format swap area Storage preparation
swapon Enable swap Virtual memory
swapoff Disable swap Maintenance
sysctl Configure swappiness Performance tuning

Swap Partition vs Swap File

Feature Swap Partition Swap File
Performance Slightly Better Very Good
Easy to Resize No Yes
Easy to Create No Yes
Cloud Friendly Limited Excellent
Enterprise Usage Common Increasingly Common

Common Swap Errors

Error Possible Cause
Permission denied Incorrect file permissions
Invalid argument Swap not formatted
Device or resource busy Swap already active
No such file Incorrect path

Production Troubleshooting Scenario

Scenario

A production application is terminated unexpectedly.

Logs show:

Out of memory

Investigation:

Check memory.

free -h

Output:

RAM: 100% Used

Swap: 0B

The server has no configured swap space.

Solution:

Create a swap file.

sudo fallocate -l 4G /swapfile

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

The server now has additional virtual memory, reducing the likelihood of future OOM events.


Best Practices

  • Configure swap on production Linux servers unless there is a specific reason not to.
  • Prefer swap files on cloud virtual machines for easier management.
  • Use secure permissions (600) on swap files.
  • Monitor swap usage regularly.
  • Tune swappiness based on workload.
  • Remember that swap complements RAM but does not replace it.

Common Mistakes

❌ Assuming swap is a replacement for physical RAM.

✅ Verify swap is a replacement for physical RAM instead of assuming it.


❌ Forgetting to add swap to /etc/fstab.

✅ Remember to to add swap to /etc/fstab.


❌ Creating swap files with insecure permissions.

✅ Avoid this mistake: creating swap files with insecure permissions.


❌ Ignoring frequent swap usage, which may indicate insufficient RAM.

✅ Always review frequent swap usage, which may indicate insufficient RAM.


❌ Setting swappiness too high for memory-sensitive workloads such as databases.

✅ Avoid this mistake: setting swappiness too high for memory-sensitive workloads such as databases.


Interview Questions

Beginner

  1. What is swap space?
  2. What is the difference between RAM and swap?
  3. Which command enables swap?
  4. How do you view active swap?

Intermediate

  1. What is the difference between a swap partition and a swap file?
  2. What is swappiness?
  3. How do you create a persistent swap file?
  4. Why should swap files have 600 permissions?

Architect Level

  1. How would you configure swap for a production Kubernetes node?
  2. How would you tune swappiness for a database server?
  3. What are the advantages and disadvantages of swap in cloud environments?

Summary

In this lesson, you learned:

  • Swap fundamentals
  • Swap partitions and swap files
  • Creating and enabling swap
  • Persistent swap configuration
  • Swappiness tuning
  • Monitoring swap usage
  • Production best practices

Swap extends physical memory by providing virtual memory on disk. Although it is much slower than RAM, it improves system stability by preventing out-of-memory conditions and providing additional memory capacity during peak workloads.


Key Takeaways

  • Swap extends RAM using disk space.
  • Linux supports both swap partitions and swap files.
  • Use mkswap to prepare swap space.
  • Use swapon and swapoff to enable and disable swap.
  • Configure persistent swap in /etc/fstab.
  • Monitor swap usage to identify memory pressure.

What's Next?

Disk Quotas — Controlling Storage Usage in Linux

You'll explore:

  • User and group disk quotas
  • Soft and hard limits
  • Enabling quotas
  • Monitoring disk usage
  • Managing quota violations
  • Enterprise storage policies

Disk quotas help administrators control storage consumption and prevent users or applications from exhausting available disk space.