How to Create and Manage a Swap File
Add swap space to your GoZen VPS to prevent out-of-memory crashes. Covers creating, resizing, and tuning swap on Ubuntu, Debian, Rocky Linux, and AlmaLinux.
Swap is disk space that your server uses as overflow memory. When physical RAM fills up, the kernel moves inactive pages to swap instead of killing processes. On a VPS with limited RAM, swap is the difference between a slow site and a crashed one.
Do You Need Swap?
| VPS RAM | Recommended Swap | Why |
|---|---|---|
| 1 GB | 1–2 GB | Essential. WordPress + MySQL alone can exceed 1 GB under load. |
| 2 GB | 2 GB | Strongly recommended. Prevents OOM kills during traffic spikes. |
| 4 GB | 2–4 GB | Recommended. Provides a safety net for memory-intensive tasks like backups. |
| 8 GB+ | 2–4 GB | Optional. Useful as a safety net but you shouldn’t rely on it. |
Swap is not a substitute for RAM. It’s much slower because it uses disk I/O. If your server regularly uses swap under normal load, you need to upgrade your VPS plan.
Check Existing Swap
Before creating swap, check if you already have some:
# Check current swap
sudo swapon --show
# Check memory and swap together
free -h
If swapon --show returns nothing, you have no swap configured.
Create a Swap File
These steps work on Ubuntu, Debian, Rocky Linux, and AlmaLinux.
Step 1: Create the File
# Create a 2 GB swap file (adjust the count for different sizes)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
| Swap Size | count Value |
|---|---|
| 1 GB | 1024 |
| 2 GB | 2048 |
| 4 GB | 4096 |
Alternatively, use fallocate (faster, but not supported on all filesystems):
sudo fallocate -l 2G /swapfile
Step 2: Set Permissions
The swap file must only be accessible by root:
sudo chmod 600 /swapfile
Step 3: Format as Swap
sudo mkswap /swapfile
Step 4: Enable the Swap File
sudo swapon /swapfile
Verify it’s active:
sudo swapon --show
free -h
You should see your new swap space listed.
Step 5: Make It Permanent
The swap file is active now, but it won’t survive a reboot unless you add it to /etc/fstab:
# Back up fstab first
sudo cp /etc/fstab /etc/fstab.bak
# Add the swap entry
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify the entry:
cat /etc/fstab
You should see a line like:
/swapfile none swap sw 0 0
Tune Swappiness
Swappiness controls how aggressively the kernel uses swap. The value ranges from 0 (use swap only when absolutely necessary) to 100 (swap aggressively).
# Check current swappiness
cat /proc/sys/vm/swappiness
The default is usually 60. For a web server, a lower value keeps more data in RAM:
| Use Case | Recommended Swappiness |
|---|---|
| Web server (WordPress, PHP) | 10 |
| Database server (MySQL, PostgreSQL) | 10 |
| General purpose | 20 |
| Default (not recommended for servers) | 60 |
Set Swappiness Temporarily
sudo sysctl vm.swappiness=10
This resets on reboot.
Set Swappiness Permanently
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
Tune VFS Cache Pressure
vfs_cache_pressure controls how aggressively the kernel reclaims memory used for caching directory and inode information. Lowering it keeps filesystem metadata in memory longer, which improves performance for workloads with many files (like a web server serving thousands of WordPress assets):
# Check current value (default is 100)
cat /proc/sys/vm/vfs_cache_pressure
# Set to 50 (keeps more metadata cached)
sudo sysctl vm.vfs_cache_pressure=50
# Make permanent
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
Resize the Swap File
To change the swap file size, you need to disable it, recreate it, and re-enable it:
# Disable current swap
sudo swapoff /swapfile
# Recreate with new size (example: 4 GB)
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
# Set permissions, format, and enable
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Verify
free -h
The /etc/fstab entry doesn’t need to change - it references the file path, not the size.
Resizing swap temporarily disables it. If your server is already under memory pressure, it may trigger OOM kills when you run swapoff. Do this during low-traffic hours.
Remove the Swap File
If you no longer need swap (for example, after upgrading to a larger VPS):
# Disable swap
sudo swapoff /swapfile
# Remove the fstab entry
sudo nano /etc/fstab
# Delete the line: /swapfile none swap sw 0 0
# Delete the file
sudo rm /swapfile
Monitor Swap Usage
Keep an eye on swap to know if your server needs more RAM:
# Quick check
free -h
# Detailed swap info
cat /proc/swaps
# Watch swap usage in real time
watch -n 5 free -h
# Which processes are using swap (sorted by usage)
for pid in /proc/[0-9]*; do
name=$(cat "$pid/comm" 2>/dev/null)
swap=$(awk '/VmSwap/{print $2}' "$pid/status" 2>/dev/null)
[ -n "$swap" ] && [ "$swap" -gt 0 ] && echo "$swap kB - $name"
done | sort -rn | head -20
Troubleshooting
| Problem | Fix |
|---|---|
swapon: /swapfile: Operation not permitted | Your VPS provider may not allow swap on certain filesystem types. Use dd instead of fallocate to create the file. |
swapon: /swapfile: Invalid argument | The file wasn’t formatted. Run sudo mkswap /swapfile first. |
| Swap not active after reboot | Check /etc/fstab for the swap entry. Make sure the line reads /swapfile none swap sw 0 0. |
| Server is slow and swap is full | Swap full = you need more RAM. Upgrade your VPS plan or optimize your applications. |
swapoff hangs | The system is trying to move swap contents back to RAM. If there isn’t enough free RAM, this will stall. Either wait, or free memory first by stopping non-essential services. |
fallocate fails | Some filesystems (like XFS with certain mount options, or older ext4) don’t support fallocate. Use dd instead. |
Related Articles
Last updated 21 Apr 2026, 08:08 +0300.