⚡ Swap Memory Executive Summary (2026)
- Why Swap is Critical: Acts as an emergency RAM safety net on NVMe SSD storage, preventing the Linux kernel Out-Of-Memory (OOM) killer from abruptly terminating MySQL and PHP processes.
- Recommended Size: 2 GB to 4 GB swap file on 1 GB / 2 GB RAM entry-level VPS instances.
- Production Tuning: Setting
vm.swappiness=10ensures physical RAM is always prioritized before utilizing disk swap.
Running resource-intensive database applications (such as WordPress with WooCommerce, MySQL 8.0, and Redis) on entry-level cloud VPS servers (1 vCPU, 1 GB to 2 GB RAM) frequently leads to sudden server crashes during traffic surges. When physical RAM hits 100%, the Linux kernel invokes the Out-Of-Memory (OOM) killer, which terminates memory-heavy background daemons — almost always killing your MySQL database.
By creating an optimized Swap File on Ubuntu 24.04 LTS, your server gains a virtual memory buffer on fast NVMe storage, keeping your web applications 100% online during traffic spikes.
In this technical masterclass, you will learn the exact step-by-step procedure to allocate, format, activate, and permanently persist swap space in under 2 minutes.
Direct Answer: How Do You Add Swap on Ubuntu 24.04?
Direct Answer: To add a 2GB swap file on Ubuntu 24.04 LTS: 1. Allocate space (sudo fallocate -l 2G /swapfile), 2. Secure permissions (sudo chmod 600 /swapfile), 3. Format as swap (sudo mkswap /swapfile), 4. Activate swap (sudo swapon /swapfile), 5. Persist in fstab (echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab), and 6. Set vm.swappiness=10 in /etc/sysctl.conf.
Recommended Swap Sizing & Kernel Tuning Matrix
| Server Physical RAM | Recommended Swap Size | Swappiness Value | Cache Pressure Setting |
|---|---|---|---|
| 512 MB RAM | 2 GB Swap File | vm.swappiness = 10 |
vm.vfs_cache_pressure = 50 |
| 1 GB RAM | 2 GB Swap File | vm.swappiness = 10 |
vm.vfs_cache_pressure = 50 |
| 2 GB RAM | 2 GB – 4 GB Swap File | vm.swappiness = 10 |
vm.vfs_cache_pressure = 50 |
| 4 GB RAM | 2 GB Swap File | vm.swappiness = 10 |
vm.vfs_cache_pressure = 50 |
| 8 GB+ RAM | 2 GB (Safety fallback) | vm.swappiness = 5 |
vm.vfs_cache_pressure = 50 |
Step-by-Step Blueprint: Creating Swap on Ubuntu 24.04
Step 1: Check for Existing Swap Space
sudo swapon --show free -h
If the output is empty, your server currently has no swap active.
Step 2: Allocate the Swap File
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
Step 3: Make Swap Permanent Across Reboots
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 4: Optimize Kernel Swappiness for NVMe Cloud VPS
Add the following parameters to /etc/sysctl.conf:
vm.swappiness=10 vm.vfs_cache_pressure=50
Apply settings immediately: sudo sysctl -p.
🔗 Recommended Related Technical Guides:
⚡ Tired of VPS Memory Crashes?
Host your website on clustered cloud infrastructure with automated memory scaling and zero maintenance on CpanelFree.com.
Frequently Asked Questions (FAQ)
❓ Does swap memory degrade NVMe SSD lifespan?
By configuring vm.swappiness=10, swap writes occur only during rare emergency memory spikes. Modern enterprise NVMe drives handle hundreds of terabytes of written data without degradation.
❓ How do I verify my swap is functioning properly?
Run free -m or htop in your terminal. You will see a dedicated Swap line displaying total, used, and free swap capacity.
❓ Can I remove the swap file later if I upgrade RAM?
Yes! Run sudo swapoff /swapfile, delete /swapfile, and remove the entry from /etc/fstab.
In-Depth Kernel Parameters for Swap Optimization
Understanding the internal mechanics of Linux virtual memory management allows sysadmins to fine-tune swap performance to match specific application workloads:
- vm.swappiness (Default: 60 | Recommended: 10): Represents the kernel’s aggressiveness in moving inactive memory pages from physical RAM to swap space. A value of 10 tells the kernel to exhaust 90% of physical RAM before utilizing disk swap.
- vm.vfs_cache_pressure (Default: 100 | Recommended: 50): Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. Lowering to 50 instructs Ubuntu to keep filesystem caches in memory longer, significantly boosting file access speed.
Configuring ZRAM: Ultra-Fast Compressed In-Memory Swap
For high-performance servers, ZRAM creates a compressed block device directly inside physical RAM. It compresses idle memory data with the LZ4 algorithm (achieving 2:1 to 3:1 compression ratios) at microsecond speeds, eliminating NVMe disk read/write latency entirely:
sudo apt install zram-tools -y echo "ALGO=lz4" | sudo tee -a /etc/default/zramswap echo "PERCENT=50" | sudo tee -a /etc/default/zramswap sudo systemctl restart zramswap
With ZRAM active, a 2 GB RAM server effectively behaves like a 3.5 GB RAM server with zero disk wear.
Troubleshooting MySQL Out-Of-Memory (OOM) Crashes
Even with swap memory enabled, an unoptimized MySQL database can consume massive amounts of RAM under high concurrent query loads. If your MySQL service continues to terminate unexpectedly, inspect the Linux system logs to confirm whether the OOM killer was triggered:
sudo dmesg -T | grep -i oom-killer sudo journalctl -u mysql -e
To prevent MySQL from exceeding available physical memory, tune the max_connections and table_open_cache directives in /etc/mysql/mysql.conf.d/mysqld.cnf. On a 1GB or 2GB VPS, setting max_connections = 100 ensures that concurrent user connections cannot overwhelm the MySQL memory buffer pool.
Setting Up Automated Swap Usage Alerts
You can create a simple cron script that monitors swap usage and sends an alert if swap consumption exceeds 70%, notifying you that it is time to optimize database queries or upgrade your server RAM:
#!/bin/bash
SWAP_USED=$(free | awk '/Swap/ {printf("%.0f"), $3/$2*100}')
if [ "$SWAP_USED" -gt 70 ]; then
echo "Warning: High Swap Usage: ${SWAP_USED}%" | mail -s "VPS Memory Alert" [email protected]
fi

