Creating and Configuring Swap Space: Complete Guide
What is Linux swap space, how to create a swap file, configure swappiness, monitor swap usage, and optimize memory performance on your server.
Table of Contents
Creating and Configuring Swap Space: Complete Guide
Swap space is virtual memory on disk that the operating system uses when physical RAM is full. It prevents system crashes under memory pressure and keeps applications running. This guide covers creating a swap file, configuring it, and monitoring usage.
Swap does not replace RAM; it only serves as an emergency buffer. Consistently high swap usage is a sign of insufficient RAM.
What Is Swap and When Is It Needed?
Swap space can be created in two ways:
| Method | Advantage | Disadvantage |
|---|---|---|
| Swap partition | Slightly faster | Fixed size |
| Swap file | Flexible, resizable | Slightly slower |
Swap files are preferred on modern systems because their size can be changed dynamically.
When is swap needed?
- RAM is 2 GB or less
- Running memory-intensive applications
- You want to use hibernate (sleep mode)
- As a safety buffer against unexpected memory spikes
Checking Current Swap Status
# Show swap usage
swapon --show
# Show memory and swap summary
free -h
# Detailed memory info
cat /proc/meminfo | grep -i swap
# List swap areas
cat /proc/swaps
Creating a Swap File
Step 1: Create the Swap File
# Create a 2 GB swap file (dd method)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
# Or with fallocate (faster)
sudo fallocate -l 2G /swapfile
# Verify file size
ls -lh /swapfile
Step 2: Set Security Permissions
# Only root can read/write
sudo chmod 600 /swapfile
# Verify permissions
ls -la /swapfile
# Output: -rw------- 1 root root 2.0G ...
Step 3: Format as Swap
# Mark as swap space
sudo mkswap /swapfile
Step 4: Enable Swap
# Activate swap
sudo swapon /swapfile
# Verify
swapon --show
free -h
Step 5: Make It Persistent
Add to /etc/fstab so swap activates automatically on reboot:
# Add to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Verify
cat /etc/fstab | grep swap
Resizing Swap
To increase the existing swap file:
# Disable swap
sudo swapoff /swapfile
# Create new file with larger size (4 GB)
sudo fallocate -l 4G /swapfile
# Reformat
sudo mkswap /swapfile
# Re-enable
sudo swapon /swapfile
# Verify
free -h
Swappiness Setting
Swappiness is a value between 0-100 that determines how aggressively the kernel uses swap:
| Value | Behavior |
|---|---|
| 0 | Use swap as little as possible |
| 10 | Recommended for servers |
| 60 | Linux default |
| 100 | Aggressive swap usage |
# Show current swappiness
cat /proc/sys/vm/swappiness
# Change temporarily (resets on reboot)
sudo sysctl vm.swappiness=10
# Change permanently
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
# Apply changes
sudo sysctl -p
swappiness=10 is recommended for servers. This minimizes swap usage before RAM is exhausted and reduces disk I/O.
Cache Pressure Setting
vfs_cache_pressure can also be tuned alongside swappiness:
# Show current value
cat /proc/sys/vm/vfs_cache_pressure
# Less aggressive cache reclaim (50 recommended)
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Monitoring Swap Usage
# Instant memory and swap status
free -h
# Continuous monitoring (every 2 seconds)
watch -n 2 free -h
# Which processes are using swap?
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $2 " " $3 "\n"}' $file
done | sort -k 2 -n -r | head -10
# Monitor with vmstat
vmstat 2 5
Understanding vmstat Output
vmstat 1 5
# si: blocks swapped in per second
# so: blocks swapped out per second
# Consistently high si/so means insufficient RAM
Disabling Swap
# Temporarily disable
sudo swapoff /swapfile
# Permanently remove
sudo swapoff /swapfile
sudo rm /swapfile
# Also remove the swap line from /etc/fstab
Before disabling swap, make sure there is enough free RAM. Otherwise the system may encounter an OOM (Out of Memory) error.
Conclusion
Swap space is important for system stability, especially on servers with limited RAM. Creating a swap file, optimizing the swappiness value, and regularly monitoring swap usage improves your server's memory management. However, swap is not a permanent solution for insufficient RAM — if you see consistently high swap usage, consider upgrading RAM.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.