{"id":573,"date":"2026-07-22T03:21:00","date_gmt":"2026-07-22T03:21:00","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=573"},"modified":"2026-10-11T22:04:09","modified_gmt":"2026-10-11T22:04:09","slug":"vps-swap-space-optimization","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/","title":{"rendered":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both approaches ignore the actual mechanics. Swap is not a performance feature; it is a safety valve that trades disk latency for the ability to survive a memory spike. Whether that trade is a good deal depends entirely on your storage medium, your workload&#8217;s access patterns, and how the kernel&#8217;s page-reclaim heuristics behave under pressure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Swap Actually Buys You<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The kernel moves pages to swap for two different reasons, and conflating them causes most of the bad advice. First, anonymous pages that have been idle can be evicted to make room for page cache \u2014 this is cheap and often improves throughput. Second, under genuine memory pressure the kernel is forced to swap out active working sets, and that is what produces the infamous multi-second stalls. You want the first behaviour and want to avoid the second. A well-tuned server rarely faults pages back in from swap except at startup after a reboot.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sizing: The Arithmetic Beats the Rule of Thumb<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The traditional &#8220;2x RAM&#8221; rule was written for systems with 512 MB of memory and spinning disks. On a modern instance the useful sizing question is: how much headroom do I need to absorb a spike while an operator intervenes? For a 1 GB instance running a web stack, 1\u20132 GB of swap is plenty; more just delays the inevitable OOM kill while thrashing the disk. For a 4 GB database host, 2\u20134 GB is a reasonable emergency reserve. If you are unsure how much memory your workload genuinely needs, this guide to <a href=\"https:\/\/virtualserversvps.com\/\">VPS plans and resource allocation<\/a> is a better starting point than guessing at swap size.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the current configuration before changing anything:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapon --show; free -m; cat \/proc\/sys\/vm\/swappiness; vmstat 1 5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>vm.swappiness<\/code> Question<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vm.swappiness<\/code> controls the relative cost the kernel assigns to reclaiming page cache versus swapping anonymous pages. It is not a simple on\/off dial. On an NVMe-backed instance a value of 10\u201320 keeps the kernel biased toward cache eviction while still allowing swap under real pressure. Setting it to 0 does not disable swapping \u2014 it only discourages it until the situation is dire, at which point reclaim becomes more aggressive and the stall is worse, not better. Measure the effect rather than copying a number: watch <code>vmstat<\/code> columns <code>si<\/code> and <code>so<\/code> during a realistic load.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Swapfile vs zram vs a Dedicated Partition<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A swapfile on the same filesystem as your data is the simplest option but shares I\/O with everything else. A zram device compresses pages in RAM, giving you effective capacity at zero disk latency \u2014 excellent for small instances where the alternative is OOM penalties, but it consumes CPU and does nothing if the compressed data would not fit. A dedicated swap partition avoids filesystem fragmentation issues but is inflexible. On most VPS workloads a modest swapfile plus a small zram device covers both the &#8220;survive the spike&#8221; and &#8220;cheap eviction&#8221; cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Measuring Whether Swap Is Helping or Hurting<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The decisive metric is not how much swap is used but the rate of swap-in. Sustained <code>si<\/code> above a few hundred KB\/s during normal operation means your working set does not fit in RAM and swap is masking a capacity problem \u2014 add memory or reduce the footprint. Occasional bursts followed by long idle periods are healthy. Combine this with <code>cat \/proc\/pressure\/memory<\/code>, which reports the percentage of time tasks were stalled on memory reclaim. If that number climbs during peak hours, swap is no longer a safety valve; it is a symptom.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Configure swap deliberately, size it from your workload rather than a slogan, and watch the pressure counters instead of the raw usage figure. Combined with the right <a href=\"https:\/\/virtualserversvps.com\/\">memory allocation for your VPS<\/a>, this keeps swap serving its purpose: an emergency buffer that buys you time, not a substitute for adequate memory.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both approaches ignore&#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":5,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-573","post","type-post","status-publish","format-standard","hentry","category-performance-optimization"],"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>Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both\" \/>\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-optimization\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure\" \/>\n<meta property=\"og:description\" content=\"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-22T03:21:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-11T22:04:09+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=\"3 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-optimization\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/\",\"name\":\"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-07-22T03:21:00+00:00\",\"dateModified\":\"2026-10-11T22:04:09+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure\"}]},{\"@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":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure - Virtual Servers VPS Blog","description":"Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both","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-optimization\/","og_locale":"en_US","og_type":"article","og_title":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure","og_description":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure","og_url":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-07-22T03:21:00+00:00","article_modified_time":"2026-10-11T22:04:09+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/","url":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/","name":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-07-22T03:21:00+00:00","dateModified":"2026-10-11T22:04:09+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"Swap on a VPS is either treated as untouchable folklore or configured on autopilot \u2014 a 2 GB swapfile because that is what a tutorial once said. Both","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/vps-swap-space-optimization\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Configuring Swap on a VPS: Sizing, swappiness, and Measuring Memory Pressure"}]},{"@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\/573","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=573"}],"version-history":[{"count":3,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":1365,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/573\/revisions\/1365"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}