Overview
Swap space is a portion of disk storage used as virtual memory when your system's physical RAM is fully utilized. It provides a safety net that prevents out-of-memory (OOM) errors and system crashes when memory-intensive applications temporarily exceed available RAM.
Cloud VPS instances typically do not come with swap pre-configured, giving you full control over whether and how much swap space to allocate based on your workload requirements.
Swap File vs Swap Partition
This guide uses swap files rather than swap partitions. Swap files are more flexible—you can resize them easily without repartitioning your disk. Performance is virtually identical on modern Linux kernels.
When to Use Swap
Good Use Cases
- • Systems with limited RAM (1-2GB)
- • Applications with variable memory usage
- • Preventing OOM killer from terminating processes
- • Hibernation support (rare on servers)
- • Running memory-intensive tasks occasionally
Considerations
- • Disk I/O is much slower than RAM
- • Heavy swapping indicates need for more RAM
- • SSD wear from frequent swap writes
- • Database servers often perform better without swap
Recommended Swap Sizes
| System RAM | Recommended Swap | Notes |
|---|---|---|
| 512MB - 1GB | 1GB - 2GB | Essential for small instances |
| 2GB - 4GB | 2GB - 4GB | Match RAM size |
| 4GB - 8GB | 2GB - 4GB | Less critical with more RAM |
| 8GB - 16GB | 2GB - 4GB | Minimal swap often sufficient |
| 16GB+ | 2GB - 4GB | Optional; consider workload |
Create a Swap File
Step 1: Check Existing Swap
First, verify if swap is already configured on your system:
sudo swapon --show
free -hIf no output from swapon --show, no swap is currently active.
Step 2: Check Available Disk Space
Ensure you have enough disk space for the swap file:
df -h /Make sure you have at least a few GB more than your intended swap size available.
Step 3: Create the Swap File
Use fallocate or dd to create the swap file. This example creates a 2GB swap file:
# Method 1: Using fallocate (faster)
sudo fallocate -l 2G /swapfile
# Method 2: Using dd (works on all filesystems)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progressNote: fallocate may not work on all filesystems (e.g., XFS with certain configurations, Btrfs). Use dd if you encounter errors.
Step 4: Set Correct Permissions
The swap file must only be accessible by root for security:
sudo chmod 600 /swapfile
ls -lh /swapfileYou should see -rw------- 1 root root in the output.
Step 5: Format as Swap
Mark the file as swap space:
sudo mkswap /swapfileEnable Swap
Activate the swap file immediately:
sudo swapon /swapfileVerify it's active:
sudo swapon --show
free -hYou should now see your swap file listed with the correct size.
Make Swap Persistent
Add the swap file to /etc/fstab so it's enabled on boot:
# Backup fstab first
sudo cp /etc/fstab /etc/fstab.backup
# Add swap entry
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabVerify the entry was added:
cat /etc/fstabYou should see a line like: /swapfile none swap sw 0 0
Tune Swappiness
The swappiness parameter controls how aggressively the kernel swaps memory pages. Values range from 0 to 100:
- 0: Swap only when absolutely necessary (avoid swapping)
- 10-30: Recommended for servers (minimize swapping)
- 60: Default for most Linux distributions
- 100: Swap aggressively
Check current value:
cat /proc/sys/vm/swappinessSet temporarily (until reboot):
sudo sysctl vm.swappiness=10Make permanent:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -pRecommended for Cloud VPS
A swappiness value of 10 is generally recommended for servers. This ensures the system prefers using RAM but can still use swap when memory pressure is high, preventing OOM conditions.
Verify Configuration
Run these commands to confirm everything is configured correctly:
# Show swap status
sudo swapon --show
# Show memory and swap usage
free -h
# Verify fstab entry
grep swap /etc/fstab
# Check swappiness setting
cat /proc/sys/vm/swappinessExpected output for a 2GB swap file:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2Remove Swap (If Needed)
If you need to remove swap later:
# Disable swap
sudo swapoff /swapfile
# Remove from fstab (edit manually or use sed)
sudo sed -i '/\/swapfile/d' /etc/fstab
# Delete the swap file
sudo rm /swapfile
# Verify
free -hBest Practices
Monitor Swap Usage
Regularly monitor swap usage with free -h or vmstat 1. Consistent high swap usage indicates you may need more RAM.
Don't Over-Allocate
More swap isn't always better. Excessive swapping severely impacts performance. If you're frequently using swap, consider upgrading your instance.
Use SSD Storage
RamNode Cloud VPS uses fast SSD/NVMe storage, making swap more viable than on traditional spinning disks. However, RAM is still significantly faster.
Database Servers
For database servers (MySQL, PostgreSQL), some DBAs prefer minimal or no swap. The database should manage its own memory. Monitor carefully in production.
Need More Memory?
If your workload consistently requires more memory than your current instance provides, consider resizing your instance for better performance than relying heavily on swap.
