{"id":1159,"date":"2026-09-17T23:07:25","date_gmt":"2026-09-17T23:07:25","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1159"},"modified":"2026-09-17T23:07:25","modified_gmt":"2026-09-17T23:07:25","slug":"transparent-hugepages-ksm-vps-latency","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/","title":{"rendered":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Two kernel memory features are enabled by default on most Linux distributions and quietly change latency characteristics in opposite directions. Transparent Hugepages (THP) reduces TLB misses by backing memory with 2&nbsp;MB pages. Kernel Samepage Merging (KSM) deduplicates identical memory pages across processes to save RAM. On a VPS, both interact with virtualisation in ways that are worth measuring rather than assuming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check What Your Kernel Is Actually Doing<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Transparent Hugepages state&#010;cat \/sys\/kernel\/mm\/transparent_hugepage\/enabled&#010;cat \/sys\/kernel\/mm\/transparent_hugepage\/defrag&#010;&#010;# Kernel Samepage Merging&#010;cat \/sys\/kernel\/mm\/ksm\/run&#010;cat \/sys\/kernel\/mm\/ksm\/pages_shared&#010;cat \/sys\/kernel\/mm\/ksm\/pages_sharing&#010;&#010;# Per-process hugepage usage&#010;grep -i huge \/proc\/meminfo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Interpretation matters here. THP has three main states: <code>always<\/code>, <code>madvise<\/code>, and <code>never<\/code>. KSM has a <code>run<\/code> value of <code>0<\/code> (off), <code>1<\/code> (on and scanning), or <code>2<\/code> (on but not started until a process opts in via <code>madvise<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why THP Is a Latency Risk for Databases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">THP&#8217;s benefit is fewer TLB misses for large, sequentially-accessed working sets. Its cost is allocation latency. When the kernel needs a 2&nbsp;MB page and none is available, it attempts compaction of physical memory \u2014 and that compaction can stall the allocating process for milliseconds to tens of milliseconds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a database with a latency-sensitive commit path, a multi-millisecond stall is worse than the TLB savings. This is why PostgreSQL, MySQL, and Redis documentation have all historically recommended <code>madvise<\/code> or <code>never<\/code> for THP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Persistent setting via kernel boot parameters&#010;# Add to GRUB_CMDLINE_LINUX_DEFAULT:&#010;#   transparent_hugepage=madvise&#010;# Then: sudo update-grub &amp;&amp; sudo reboot&#010;&#010;# Runtime (non-persistent) for testing:&#010;echo madvise | sudo tee \/sys\/kernel\/mm\/transparent_hugepage\/enabled&#010;echo defer   | sudo tee \/sys\/kernel\/mm\/transparent_hugepage\/defrag<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Setting <code>defrag<\/code> to <code>defer<\/code> is the low-risk middle ground: pages are still merged opportunistically in the background, but no process is blocked waiting for compaction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">KSM on a VPS: Deduplication That Costs CPU<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">KSM scans memory pages and merges those with identical content into a single copy-on-write page. On a host running many similar VMs, this can reclaim substantial RAM \u2014 which is why hosts sometimes enable it even when your guest has it disabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The trade-off is CPU. The scanning daemon consumes cycles, and when a merged page is written, the guest takes a copy-on-write fault. Those faults are usually fast, but under memory pressure with thousands of merged pages, they add up.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>RAM effect<\/th><th>CPU effect<\/th><th>Latency risk<\/th><\/tr><\/thead><tbody><tr><td>THP always<\/td><td>May use more RAM per mapping<\/td><td>Lower TLB overhead<\/td><td>High \u2014 compaction stalls<\/td><\/tr><tr><td>THP madvise<\/td><td>Neutral<\/td><td>Neutral<\/td><td>Low<\/td><\/tr><tr><td>KSM on<\/td><td>Frees duplicate pages<\/td><td>Scan + COW fault overhead<\/td><td>Low to moderate<\/td><\/tr><tr><td>KSM off<\/td><td>None<\/td><td>None<\/td><td>None<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Measuring the Impact on Your Workload<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Do not change these settings based on general advice. Measure your own server. A simple before\/after procedure:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Record a baseline. Run your normal workload and capture latency percentiles \u2014 for a database, <code>pgbench<\/code> or <code>sysbench<\/code>; for a web app, request p95 and p99 from your access log.<\/li><li>Check the current state and note it.<\/li><li>Change one setting at a time. Toggle THP to <code>madvise<\/code> first, re-run the benchmark, compare.<\/li><li>Then toggle KSM and repeat. Do not change both at once or you cannot attribute the effect.<\/li><li>Watch <code>pages_sharing<\/code> after enabling KSM \u2014 if it stays near zero, the feature is costing CPU for no benefit on your workload.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">For latency-sensitive databases, the common configuration is THP set to <code>madvise<\/code> with <code>defrag=defer<\/code>, and KSM left off in the guest unless memory pressure is demonstrable. For memory-constrained multi-tenant workloads \u2014 several small containers on one VPS \u2014 KSM enabled in the guest can reclaim real RAM at an acceptable CPU cost.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interaction With Virtualisation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inside a KVM guest, the host decides whether to back your guest memory with hugepages at its own level. Your guest-level THP setting controls only what the guest kernel requests from the virtualised MMU. The host&#8217;s own THP policy can still introduce latency you cannot see from inside. This is one more reason to measure end-to-end response time rather than reasoning from guest settings alone. A host that enables aggressive host-level THP can produce latency spikes in your guest that no amount of guest-level tuning will remove, because you are seeing the effect of host memory compaction on the physical pages backing your VM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Detecting a Host-Level Stall<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If your application shows periodic latency spikes that do not correlate with your own CPU, disk, or memory activity, check two things. First, whether the spikes align with a regular interval such as 30 or 60 seconds, which often indicates a periodic host operation. Second, whether <em>steal<\/em> time in <code>mpstat<\/code> rises at the same moments. If steal rises while your guest&#8217;s own CPU usage is flat, the stall originated outside your VM.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You cannot fix a host-level policy from inside the guest, but you can prove it is happening and raise it with evidence. What you can control is your own configuration: monitor <code>compact_stall<\/code> and <code>compact_fail<\/code> counters in <code>\/proc\/vmstat<\/code>. A rising <code>compact_stall<\/code> count is direct evidence that allocation is blocking on memory compaction, which correlates closely with THP-induced latency.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Direct evidence of compaction-induced stalls&#010;grep -E 'compact_stall|compact_fail|compact_success' \/proc\/vmstat&#010;&#010;# Steal time while a latency spike is occurring&#010;mpstat 1 20 | tail -20<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>compact_stall<\/code> is climbing, the fix is the guest-side THP change to <code>madvise<\/code> with <code>defer<\/code>, which stops the kernel performing synchronous compaction in the allocation path. That single change removes the most common in-guest source of multi-millisecond pauses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you rent a full-virtualisation instance such as those at <a href=\"https:\/\/virtualserversvps.com\/\">virtualserversvps.com<\/a>, you have your own kernel and can set these parameters directly rather than depending on a shared host&#8217;s policy \u2014 which makes this class of tuning both possible and worth doing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recommendations<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Database and latency-sensitive servers: THP <code>madvise<\/code>, defrag <code>defer<\/code>, KSM off.<\/li><li>Memory-constrained multi-service boxes: KSM on, verify <code>pages_sharing<\/code> is non-trivial.<\/li><li>Compute-heavy workloads with large sequential working sets: THP <code>always<\/code> can genuinely help \u2014 measure before dismissing it.<\/li><li>Always benchmark rather than applying defaults from a blog post, including this one.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Two kernel memory features are enabled by default on most Linux distributions and quietly change latency characteristics in opposite directions. Transparent Hugepages (THP) reduces TLB misses by backing memory with&#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":1,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1159","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>Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured - 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\/transparent-hugepages-ksm-vps-latency\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured\" \/>\n<meta property=\"og:description\" content=\"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-17T23:07:25+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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/\",\"name\":\"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-09-17T23:07:25+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured\"}]},{\"@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":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured - 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\/transparent-hugepages-ksm-vps-latency\/","og_locale":"en_US","og_type":"article","og_title":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured","og_description":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured","og_url":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-09-17T23:07:25+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/","url":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/","name":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-09-17T23:07:25+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/transparent-hugepages-ksm-vps-latency\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Transparent Hugepages and Kernel Samepage Merging on a VPS: Latency Effects Measured"}]},{"@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\/1159","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=1159"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1159\/revisions"}],"predecessor-version":[{"id":1161,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1159\/revisions\/1161"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}