{"id":443,"date":"2026-06-17T07:00:58","date_gmt":"2026-06-17T07:00:58","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=443"},"modified":"2026-06-22T02:38:20","modified_gmt":"2026-06-22T02:38:20","slug":"vps-swap-space-configure-optimize-linux-2","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/","title":{"rendered":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Swap space extends your VPS&#8217;s effective memory by moving idle pages from RAM to disk when physical memory runs low. Without swap, an out-of-memory (OOM) situation can kill critical processes like MySQL, Nginx, or your web application. However, swap on slow storage can cripple performance. This guide covers modern swap strategies \u2014 swap files, swappiness tuning, and ZRAM compression \u2014 to keep your VPS stable and fast.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a full breakdown of VPS plans with sufficient RAM for your workload, <a href=\"https:\/\/virtualserversvps.com\/#performance\">check our performance benchmarks<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Swap File vs. Swap Partition: Modern Best Practice<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Historically, Linux used swap <em>partitions<\/em> \u2014 dedicated disk partitions for swap data. Modern distributions prefer swap <em>files<\/em> because they are easier to resize, don&#8217;t require repartitioning, and perform identically under the Linux kernel (since kernel 5.0, swap file performance is on par with swap partitions on all filesystems).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For cloud VPS environments running ext4 or XFS, a swap file is the recommended approach. If your VPS uses Btrfs or ZFS, you may still need a swap partition \u2014 check your distribution&#8217;s documentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Much Swap Do You Need?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The old rule of thumb (&#8220;2\u00d7 RAM&#8221;) was designed for servers with spinning HDDs and small memories. Modern VPS servers with SSDs or NVMe storage need less swap because they have more RAM. Use these updated recommendations:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>RAM Size<\/th><th>Recommended Swap<\/th><th>With ZRAM + Swap<\/th><th>Use Case<\/th><\/tr><\/thead><tbody><tr><td>512 MB<\/td><td>1 GB<\/td><td>768 MB<\/td><td>Micro instances, lightweight proxies<\/td><\/tr><tr><td>1 GB<\/td><td>1 GB<\/td><td>512 MB<\/td><td>Small WordPress, API backends<\/td><\/tr><tr><td>2 GB<\/td><td>1 GB<\/td><td>512 MB<\/td><td>Medium web apps, Docker hosts<\/td><\/tr><tr><td>4 GB<\/td><td>1\u20132 GB<\/td><td>512 MB<\/td><td>Database servers, CI runners<\/td><\/tr><tr><td>8 GB+<\/td><td>1\u20132 GB<\/td><td>0\u2013512 MB<\/td><td>Heavy workloads, production<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If your VPS has NVMe storage (10\u00d7 faster than SATA SSD), you can rely on swap more aggressively. If you&#8217;re on HDD-based VPS, avoid swap entirely and <a href=\"https:\/\/virtualserversvps.com\/#performance\">compare specs on our site<\/a> for SSD-based alternatives.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Swap File on Your Linux VPS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These commands create a 2 GB swap file on a standard Linux VPS (Ubuntu 22.04+, Debian 12+, Rocky Linux 9+):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a 2GB swap file\nsudo fallocate -l 2G \/swapfile\n\n# Restrict permissions (root only)\nsudo chmod 600 \/swapfile\n\n# Format as swap\nsudo mkswap \/swapfile\n\n# Enable swap immediately\nsudo swapon \/swapfile\n\n# Make permanent across reboots\necho '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab\n\n# Verify it's active\nsudo swapon --show\nfree -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note on fallocate:<\/strong> On some filesystems (XFS on older kernels), <code>fallocate<\/code> creates a sparse file that can cause &#8220;swap area header corrupted&#8221; errors. If you encounter this, use <code>dd<\/code> instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=2048 status=progress<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Swappiness: The Most Important Swap Performance Tuning<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> kernel parameter controls how aggressively Linux uses swap. It ranges from 0 (never swap unless absolutely necessary) to 100 (swap aggressively). The default is 60 \u2014 too high for most VPS workloads.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Recommended swappiness values by workload:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Workload<\/th><th>Swappiness<\/th><th>Rationale<\/th><\/tr><\/thead><tbody><tr><td>Database servers (MySQL, PostgreSQL)<\/td><td>1\u20135<\/td><td>Keep all data in RAM, swap only under OOM pressure<\/td><\/tr><tr><td>Web servers (Nginx, Apache)<\/td><td>10<\/td><td>Balance \u2014 swap rarely, keep cached files in RAM<\/td><\/tr><tr><td>General applications<\/td><td>10\u201320<\/td><td>Standard recommendation for VPS<\/td><\/tr><tr><td>Desktop\/file server<\/td><td>30\u201360<\/td><td>Background processes can swap harmlessly<\/td><\/tr><tr><td>Memory testing \/ low-RAM VPS<\/td><td>1<\/td><td>Preserve every MB of RAM for active processes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To set swappiness to 10 immediately and permanently:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Apply immediately\nsudo sysctl vm.swappiness=10\n\n# Make permanent\necho 'vm.swappiness=10' | sudo tee -a \/etc\/sysctl.conf\n\n# Verify\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Supercharging Swap with ZRAM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ZRAM creates a compressed block device in RAM that acts as swap space. Because compressed data takes less room, a 512 MB ZRAM device can hold 1.5\u20132 GB of uncompressed memory pages. This is faster than disk swap (no I\/O) and more efficient than unused idle RAM.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On Ubuntu\/Debian:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install zram-tools\n# Edit \/etc\/default\/zramswap and set PERCENT=50 (uses 50% of RAM)\nsudo systemctl restart zramswap\nswapon --show  # You'll see \/dev\/zram0 as swap<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With ZRAM active, set swappiness to 100 so the kernel prefers the fast compressed RAM swap over disk swap. This is the optimal configuration for budget VPS instances with 1\u20132 GB RAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Monitoring Swap Usage in Real Time<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use these tools to track swap health:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>free -h<\/code> \u2014 Quick total\/used\/free swap overview<\/li><li><code>vmstat 5<\/code> \u2014 Shows <code>si<\/code> (swap in) and <code>so<\/code> (swap out) per second. Consistently high values (100+ MB\/s) mean you need more RAM<\/li><li><code>top -o %MEM<\/code> \u2014 Identify which processes are consuming the most memory<\/li><li><code>cat \/proc\/meminfo | grep -i swap<\/code> \u2014 Detailed swap statistics including <code>SwapCached<\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>vmstat<\/code> shows sustained swap activity (si\/so over 50 MB\/s for extended periods), your VPS is under memory pressure. Consider upgrading your plan or <a href=\"https:\/\/virtualserversvps.com\/\">see our VPS comparison table<\/a> for plans with more RAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Swap Pitfalls<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Swap on HDD:<\/strong> HDD-backed swap causes severe latency spikes. Avoid entirely.<\/li><li><strong>Too much swap:<\/strong> Oversized swap masks memory leaks. Fix the leak instead.<\/li><li><strong>Multiple swap devices:<\/strong> Linux prioritizes by priority value (<code>swapon -p<\/code>). Set ZRAM higher than disk swap.<\/li><li><strong>Swap on Btrfs:<\/strong> Requires <code>nodatacow<\/code> attribute on the swap file and kernel 5.0+.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Swap space extends your VPS&#8217;s effective memory by moving idle pages from RAM to disk when physical memory runs low. Without swap, an out-of-memory (OOM) situation can kill critical processes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM - Virtual Servers VPS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM\" \/>\n<meta property=\"og:description\" content=\"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-06-17T07:00:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-22T02:38:20+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/\",\"name\":\"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-06-17T07:00:58+00:00\",\"dateModified\":\"2026-06-22T02:38:20+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM - Virtual Servers VPS Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/","og_locale":"en_US","og_type":"article","og_title":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM","og_description":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM","og_url":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-06-17T07:00:58+00:00","article_modified_time":"2026-06-22T02:38:20+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/","url":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/","name":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-06-17T07:00:58+00:00","dateModified":"2026-06-22T02:38:20+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-configure-optimize-linux-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"VPS Swap Space Optimization: Smart Configuration with Swappiness Tuning and ZRAM"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":2,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":481,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions\/481"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}