How to Add Swap Memory on Ubuntu 24.04 VPS (Prevent Out of Memory Crashes)

⚡ Swap Memory Executive Summary (2026)

🎯 Difficulty: Intermediate
⚡ Est. Execution: 10–15 Mins
⏱️ 7 min read (1,225 words)

🔄 Verified September 2026 Edition

🐧 Target Environment: Ubuntu 24.04 / 22.04 LTS • Debian 12 • AlmaLinux 9
🛡️ Reviewed by: Senior Linux Systems Architect

  • 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=10 ensures 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.

📖 Benchmark Guide: Want to test your NVMe speed? Read our tutorial on How to Benchmark Your VPS CPU, Disk NVMe & Network Speed in 60 Seconds.

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

Zero Server Overhead

Prefer Not to Manage Commands via Terminal?

Deploy high-speed hosting with CpanelFree $0 Free Web Hosting. Enjoy authentic cPanel, unmetered NVMe SSD storage, PHP 8.3, and automatic SSL without server maintenance.

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.

⚡ Tired of VPS Memory Crashes?

Host your website on clustered cloud infrastructure with automated memory scaling and zero maintenance on CpanelFree.com.

Get Free Clustered Hosting →


Frequently Asked Questions (FAQ)

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:

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
🚀 Production Cloud Infrastructure

Deploy on High-Performance Cloud VPS with MeraHost

Need dedicated vCPU, root SSH access, pure NVMe SSD speeds, and 99.95% uptime SLA for your production stack? Launch with MeraHost — guaranteed price lock with zero renewal hikes.

Leave a Comment