{"id":1281,"date":"2026-09-03T09:36:08","date_gmt":"2026-09-03T04:06:08","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-add-swap-memory-ubuntu-24-04-vps\/"},"modified":"2026-09-03T12:33:01","modified_gmt":"2026-09-03T07:03:01","slug":"how-to-add-swap-memory-ubuntu-24-04-vps","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-add-swap-memory-ubuntu-24-04-vps\/","title":{"rendered":"How to Add Swap Memory on Ubuntu 24.04 VPS (Prevent Out of Memory Crashes)"},"content":{"rendered":"<div style=\"background-color: #f0f7ff;border-left: 4px solid #0066cc;padding: 18px 22px;margin-bottom: 28px;border-radius: 6px\">\n<p style=\"margin: 0 0 10px 0;font-weight: bold;color: #004499;font-size: 1.15em\">\u26a1 Swap Memory Executive Summary (2026)<\/p>\n<ul style=\"margin: 0;padding-left: 20px;color: #333;line-height: 1.7\">\n<li><strong>Why Swap is Critical:<\/strong> 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.<\/li>\n<li><strong>Recommended Size:<\/strong> 2 GB to 4 GB swap file on 1 GB \/ 2 GB RAM entry-level VPS instances.<\/li>\n<li><strong>Production Tuning:<\/strong> Setting <code>vm.swappiness=10<\/code> ensures physical RAM is always prioritized before utilizing disk swap.<\/li>\n<\/ul>\n<\/div>\n<p>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 <strong>Out-Of-Memory (OOM) killer<\/strong>, which terminates memory-heavy background daemons &mdash; almost always killing your MySQL database.<\/p>\n<p>By creating an optimized <strong>Swap File on Ubuntu 24.04 LTS<\/strong>, your server gains a virtual memory buffer on fast NVMe storage, keeping your web applications 100% online during traffic spikes.<\/p>\n<p>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.<\/p>\n<hr style=\"margin: 30px 0;border: none;border-top: 1px solid #e2e8f0\" \/>\n<h2>Direct Answer: How Do You Add Swap on Ubuntu 24.04?<\/h2>\n<p><strong>Direct Answer:<\/strong> To add a 2GB swap file on Ubuntu 24.04 LTS: 1. Allocate space (<code>sudo fallocate -l 2G \/swapfile<\/code>), 2. Secure permissions (<code>sudo chmod 600 \/swapfile<\/code>), 3. Format as swap (<code>sudo mkswap \/swapfile<\/code>), 4. Activate swap (<code>sudo swapon \/swapfile<\/code>), 5. Persist in fstab (<code>echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/code>), and 6. Set <code>vm.swappiness=10<\/code> in <code>\/etc\/sysctl.conf<\/code>.<\/p>\n<div style=\"background-color: #f8fafc;border-left: 4px solid #0284c7;padding: 12px 16px;margin: 18px 0;border-radius: 4px\">\n    \ud83d\udcd6 <strong>Benchmark Guide:<\/strong> Want to test your NVMe speed? Read our tutorial on <a href=\"https:\/\/cpanelfree.com\/blog\/how-to-benchmark-vps-cpu-disk-network-speed\/\" style=\"color: #0284c7;font-weight: bold;text-decoration: underline\">How to Benchmark Your VPS CPU, Disk NVMe &amp; Network Speed in 60 Seconds<\/a>.\n<\/div>\n<hr style=\"margin: 30px 0;border: none;border-top: 1px solid #e2e8f0\" \/>\n<h2>Recommended Swap Sizing &amp; Kernel Tuning Matrix<\/h2>\n<table style=\"width: 100%;border-collapse: collapse;margin-top: 15px;margin-bottom: 25px;font-size: 14px;text-align: left\">\n<thead>\n<tr style=\"background-color: #f1f5f9;border-bottom: 2px solid #cbd5e1\">\n<th style=\"padding: 10px;border: 1px solid #cbd5e1\">Server Physical RAM<\/th>\n<th style=\"padding: 10px;border: 1px solid #cbd5e1\">Recommended Swap Size<\/th>\n<th style=\"padding: 10px;border: 1px solid #cbd5e1\">Swappiness Value<\/th>\n<th style=\"padding: 10px;border: 1px solid #cbd5e1\">Cache Pressure Setting<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><strong>512 MB RAM<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">2 GB Swap File<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.swappiness = 10<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.vfs_cache_pressure = 50<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><strong>1 GB RAM<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">2 GB Swap File<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.swappiness = 10<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.vfs_cache_pressure = 50<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><strong>2 GB RAM<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">2 GB &#8211; 4 GB Swap File<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.swappiness = 10<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.vfs_cache_pressure = 50<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><strong>4 GB RAM<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">2 GB Swap File<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.swappiness = 10<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.vfs_cache_pressure = 50<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><strong>8 GB+ RAM<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">2 GB (Safety fallback)<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.swappiness = 5<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>vm.vfs_cache_pressure = 50<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr style=\"margin: 30px 0;border: none;border-top: 1px solid #e2e8f0\" \/>\n<h2>Step-by-Step Blueprint: Creating Swap on Ubuntu 24.04<\/h2>\n<h3>Step 1: Check for Existing Swap Space<\/h3>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo swapon --show\nfree -h<\/pre>\n<p>If the output is empty, your server currently has no swap active.<\/p>\n<h3>Step 2: Allocate the Swap File<\/h3>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo fallocate -l 2G \/swapfile\nsudo chmod 600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/pre>\n<h3>Step 3: Make Swap Permanent Across Reboots<\/h3>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/pre>\n<h3>Step 4: Optimize Kernel Swappiness for NVMe Cloud VPS<\/h3>\n<p>Add the following parameters to <code>\/etc\/sysctl.conf<\/code>:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">vm.swappiness=10\nvm.vfs_cache_pressure=50<\/pre>\n<p>Apply settings immediately: <code>sudo sysctl -p<\/code>.<\/p>\n<div style=\"background-color: #f8fafc;border: 1px solid #e2e8f0;border-left: 4px solid #0ea5e9;padding: 20px;border-radius: 8px;margin: 30px 0\">\n<h3 style=\"margin-top: 0;color: #0f172a;font-size: 18px;display: flex;align-items: center\">\n        <span style=\"margin-right: 8px\">\ud83d\udd17<\/span> Recommended Related Technical Guides:<br \/>\n    <\/h3>\n<ul style=\"margin: 10px 0 0 0;padding-left: 20px;line-height: 1.8\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-get-free-cloud-vps-forever\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Get a Free Cloud VPS Forever<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/oracle-cloud-always-free-vps-setup-guide\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Oracle Cloud Always Free VPS Setup Guide<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/best-cheap-cloud-vps-providers-2026\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Top 7 Best Cheap Cloud VPS Providers in 2026<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/best-free-web-hosting-2026\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Top 10 Best Free Web Hosting Services<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Explore $0 Free cPanel Web Hosting Plans<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #07131E 0%, #1e293b 100%);color: #ffffff;padding: 24px 22px;border-radius: 12px;margin: 25px 0\">\n<h4 style=\"color: #38bdf8;margin-top: 0;font-size: 1.2em\">\u26a1 Tired of VPS Memory Crashes?<\/h4>\n<p style=\"color: #cbd5e1;line-height: 1.6;margin-bottom: 16px\">Host your website on clustered cloud infrastructure with automated memory scaling and zero maintenance on <strong>CpanelFree.com<\/strong>.<\/p>\n<p>    <a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"background: #0EA5E9;color: #ffffff;padding: 10px 24px;border-radius: 9999px;font-weight: 700;text-decoration: none\">Get Free Clustered Hosting &rarr;<\/a>\n<\/div>\n<hr style=\"margin: 35px 0;border: none;border-top: 1px solid #e2e8f0\" \/>\n<h2>Frequently Asked Questions (FAQ)<\/h2>\n<div style=\"margin-bottom: 16px;border: 1px solid #e2e8f0;border-radius: 8px;padding: 18px 20px;background-color: #fafbfc\">\n<h3 style=\"margin: 0 0 10px 0;font-size: 1.1em;color: #1e293b\">\u2753 Does swap memory degrade NVMe SSD lifespan?<\/h3>\n<p style=\"margin: 0;color: #475569;line-height: 1.6\">By configuring <code>vm.swappiness=10<\/code>, swap writes occur only during rare emergency memory spikes. Modern enterprise NVMe drives handle hundreds of terabytes of written data without degradation.<\/p>\n<\/div>\n<div style=\"margin-bottom: 16px;border: 1px solid #e2e8f0;border-radius: 8px;padding: 18px 20px;background-color: #fafbfc\">\n<h3 style=\"margin: 0 0 10px 0;font-size: 1.1em;color: #1e293b\">\u2753 How do I verify my swap is functioning properly?<\/h3>\n<p style=\"margin: 0;color: #475569;line-height: 1.6\">Run <code>free -m<\/code> or <code>htop<\/code> in your terminal. You will see a dedicated Swap line displaying total, used, and free swap capacity.<\/p>\n<\/div>\n<div style=\"margin-bottom: 25px;border: 1px solid #e2e8f0;border-radius: 8px;padding: 18px 20px;background-color: #fafbfc\">\n<h3 style=\"margin: 0 0 10px 0;font-size: 1.1em;color: #1e293b\">\u2753 Can I remove the swap file later if I upgrade RAM?<\/h3>\n<p style=\"margin: 0;color: #475569;line-height: 1.6\">Yes! Run <code>sudo swapoff \/swapfile<\/code>, delete <code>\/swapfile<\/code>, and remove the entry from <code>\/etc\/fstab<\/code>.<\/p>\n<\/div>\n<h2>In-Depth Kernel Parameters for Swap Optimization<\/h2>\n<p>Understanding the internal mechanics of Linux virtual memory management allows sysadmins to fine-tune swap performance to match specific application workloads:<\/p>\n<ul style=\"padding-left: 20px;line-height: 1.8\">\n<li><strong>vm.swappiness (Default: 60 | Recommended: 10):<\/strong> Represents the kernel&#8217;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.<\/li>\n<li><strong>vm.vfs_cache_pressure (Default: 100 | Recommended: 50):<\/strong> 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.<\/li>\n<\/ul>\n<h2>Configuring ZRAM: Ultra-Fast Compressed In-Memory Swap<\/h2>\n<p>For high-performance servers, <strong>ZRAM<\/strong> 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:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo apt install zram-tools -y\necho \"ALGO=lz4\" | sudo tee -a \/etc\/default\/zramswap\necho \"PERCENT=50\" | sudo tee -a \/etc\/default\/zramswap\nsudo systemctl restart zramswap<\/pre>\n<p>With ZRAM active, a 2 GB RAM server effectively behaves like a 3.5 GB RAM server with zero disk wear.<\/p>\n<h2>Troubleshooting MySQL Out-Of-Memory (OOM) Crashes<\/h2>\n<p>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:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo dmesg -T | grep -i oom-killer\nsudo journalctl -u mysql -e<\/pre>\n<p>To prevent MySQL from exceeding available physical memory, tune the <code>max_connections<\/code> and <code>table_open_cache<\/code> directives in <code>\/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/code>. On a 1GB or 2GB VPS, setting <code>max_connections = 100<\/code> ensures that concurrent user connections cannot overwhelm the MySQL memory buffer pool.<\/p>\n<h2>Setting Up Automated Swap Usage Alerts<\/h2>\n<p>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:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">#!\/bin\/bash\nSWAP_USED=$(free | awk '\/Swap\/ {printf(\"%.0f\"), $3\/$2*100}')\nif [ \"$SWAP_USED\" -gt 70 ]; then\n    echo \"Warning: High Swap Usage: ${SWAP_USED}%\" | mail -s \"VPS Memory Alert\" admin@yourdomain.com\nfi<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Step-by-step guide to creating and optimizing a swap file on Ubuntu 24.04 LTS to stop MySQL crashes and Out-Of-Memory (OOM) errors on 1GB\/2GB VPS.<\/p>\n","protected":false},"author":1,"featured_media":1348,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[100,111,112,110,52],"class_list":["post-1281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-vps","tag-linux-vps","tag-oom-killer","tag-performance","tag-swap-memory","tag-ubuntu-24-04"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=1281"}],"version-history":[{"count":4,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1281\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1281\/revisions\/1569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1348"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}