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¶
Course Progress
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:
With swap:
What is Swap?¶
Swap is disk space used as an extension of physical memory (RAM).
Memory hierarchy:
RAM is much faster than swap, but swap provides additional virtual memory when needed.
How Swap Works¶
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:
Advantages:
- Better performance
- Common in enterprise deployments
- Traditional approach
Swap File¶
A regular file used as swap.
Example:
Advantages:
- Easy to create
- Easy to resize
- Common in cloud virtual machines
View Swap Information¶
Display swap usage.
Example:
View Memory Usage¶
Example:
Create a Swap File¶
Create a 2 GB swap file.
If fallocate is unavailable:
Set Secure Permissions¶
Only the root user should have access.
Format as Swap¶
Example output:
Enable Swap¶
Verify:
Disable Swap¶
Configure Persistent Swap¶
Add the following entry to:
After reboot, the swap file will be enabled automatically.
Create a Swap Partition¶
Format the partition.
Enable it.
View Swap UUID¶
Example:
Persistent /etc/fstab entry:
Swappiness¶
Linux uses the swappiness parameter to determine how aggressively it moves memory pages to swap.
View current value.
Example:
Temporarily change it.
Persist the setting.
Edit:
Add:
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.
View memory.
Create swap.
Enable swap.
Disable swap.
Real Production Examples¶
Check swap usage.
Enable swap.
Monitor memory.
Adjust swappiness.
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.
Task 2¶
Display active swap.
Task 3¶
Create a 1 GB swap file.
Task 4¶
Set permissions.
Task 5¶
Format the swap file.
Task 6¶
Enable swap.
Task 7¶
Verify swap.
Task 8¶
Disable swap.
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:
Investigation:
Check memory.
Output:
The server has no configured swap space.
Solution:
Create a swap file.
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¶
- What is swap space?
- What is the difference between RAM and swap?
- Which command enables swap?
- How do you view active swap?
Intermediate¶
- What is the difference between a swap partition and a swap file?
- What is swappiness?
- How do you create a persistent swap file?
- Why should swap files have
600permissions?
Architect Level¶
- How would you configure swap for a production Kubernetes node?
- How would you tune swappiness for a database server?
- 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
mkswapto prepare swap space. - Use
swaponandswapoffto 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.