Skip to main content
Back to Category

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.

Read time: 9 min Server Management
swapvirtual memoryswappinesslinuxmemoryperformanceserver

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:

MethodAdvantageDisadvantage
Swap partitionSlightly fasterFixed size
Swap fileFlexible, resizableSlightly 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

hljs bash
# 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

hljs bash
# 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

hljs bash
# 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

hljs bash
# Mark as swap space
sudo mkswap /swapfile

Step 4: Enable Swap

hljs bash
# 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:

hljs bash
# 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:

hljs bash
# 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:

ValueBehavior
0Use swap as little as possible
10Recommended for servers
60Linux default
100Aggressive swap usage
hljs bash
# 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:

hljs bash
# 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

hljs bash
# 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

hljs bash
vmstat 1 5
# si: blocks swapped in per second
# so: blocks swapped out per second
# Consistently high si/so means insufficient RAM

Disabling Swap

hljs bash
# 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.