{"id":1715,"date":"2026-11-15T10:40:03","date_gmt":"2026-11-15T10:40:03","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1715"},"modified":"2026-11-15T10:40:03","modified_gmt":"2026-11-15T10:40:03","slug":"thp-madvise-khugepaged-transparent-huge-pages-vps-p99","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/","title":{"rendered":"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Transparent Huge Pages let the kernel back memory with 2 MB pages instead of 4 KB pages, cutting TLB misses and page-table walk depth. On bare metal this is usually a small win. On a virtualized guest it is a different calculation: 2 MB pages map to the hypervisor&#8217;s memory management in ways that can add swap-in cost, extend allocation stalls, and \u2014 under the wrong mode \u2014 turn a routine <code>malloc<\/code> into a millisecond-scale event.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The failure mode is subtle. THP does not usually make throughput worse; it makes the tail of the latency distribution worse. Median request times stay flat while p99.9 doubles, and the symptom looks like a database tuning problem rather than a page-size problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The three THP modes and what each actually does<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">THP is controlled per-system through <code>\/sys\/kernel\/mm\/transparent_hugepage\/enabled<\/code>, which accepts three values: <code>always<\/code>, <code>madvise<\/code>, and <code>never<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/sys\/kernel\/mm\/transparent_hugepage\/enabled\n# [always] madvise never\ncat \/sys\/kernel\/mm\/transparent_hugepage\/defrag\n# [always] defer defer+madvise madvise never<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In <code>always<\/code> mode, any anonymous mapping large enough can be backed by a 2 MB page the moment it is faulted in, with no cooperation from the application. In <code>madvise<\/code> mode, allocation only happens where the program calls <code>madvise(MADV_HUGEPAGE)<\/code> on a range \u2014 the default for most modern allocators is to ask for it only on large arenas. In <code>never<\/code> mode, THP is off entirely and every page is 4 KB.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second file matters as much as the first. <code>defrag<\/code> governs what the kernel does when a 2 MB page cannot be formed immediately. Under <code>always<\/code>, direct compaction runs synchronously in the faulting thread, which is exactly the multi-millisecond stall that shows up as a latency spike.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Mode<\/th><th>Who requests THP<\/th><th>Typical effect<\/th><th>Best fit<\/th><\/tr><\/thead><tbody><tr><td><code>always<\/code><\/td><td>Kernel, automatically<\/td><td>Higher throughput, longer tail latencies<\/td><td>Batch analytics, stateless compute<\/td><\/tr><tr><td><code>madvise<\/code><\/td><td>Application explicitly<\/td><td>Opt-in wins on hot mappings only<\/td><td>Mixed workloads, most web stacks<\/td><\/tr><tr><td><code>never<\/code><\/td><td>Nobody<\/td><td>Predictable 4 KB behaviour<\/td><td>Databases with tight fsync SLAs<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">khugepaged: the background collapse<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Even in <code>madvise<\/code> mode, <code>khugepaged<\/code> scans memory and collapses contiguous 4 KB pages into 2 MB pages opportunistically. It runs with a target scan rate (<code>pages_to_scan<\/code>, default 4096) and sleep interval (<code>alloc_sleep_millisecs<\/code>, default 60000). The work is done on behalf of whichever process owns the pages, so the cost lands on your application, not on a neutral background thread.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps -o pid,comm,pri,ni -C khugepaged\ngrep -E 'pages_to_scan|alloc_sleep|scan_sleep' \\\n  \/sys\/kernel\/mm\/transparent_hugepage\/khugepaged\/*\n# collapse count since boot\ngrep -E 'thp_collapse_alloc|thp_fault_alloc' \/proc\/vmstat<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The two <code>vmstat<\/code> counters are the clearest signal of whether THP is actually doing anything. <code>thp_fault_alloc<\/code> counts huge pages allocated on fault; <code>thp_collapse_alloc<\/code> counts ones produced by khugepaged. If both are flat while your RSS grows, THP is effectively dormant and it is not the cause of your problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Measuring the effect on tail latency<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You cannot judge THP from averages. Run a fixed workload under each mode and compare percentiles. A repeatable procedure on a VPS with PostgreSQL or MySQL works like this: pin to a quiet period, run the same read benchmark three times per mode, and record p50, p99, and p99.9.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># per mode: echo madvise &gt; ...\/enabled ; then\nsysbench oltp_read_only --threads=8 --time=120 \\\n  --rand-type=uniform --report-interval=10 run | tee out.$MODE\n\n# page-fault and TLB cost attribution\nperf stat -e dTLB-load-misses,dTLB-store-misses,\\\npage-faults,minor-faults -p $(pgrep -n mysqld) sleep 30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Two patterns are typical. With <code>always<\/code>, <code>dTLB-load-misses<\/code> drops by 60\u201390% and it is easy to conclude THP is free. But <code>perf record -e kmem:mm_page_alloc -g<\/code> during the same run will often show compaction and allocation stalls inside worker threads, and that is where the p99 regression comes from. With <code>never<\/code>, TLB misses rise but the distribution tightens. Which trade you want depends on whether you are optimized for throughput or for a latency promise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why VPS guests behave differently from bare metal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Three virtualization-specific effects change the calculus. First, guest physical memory is host virtual memory; a 2 MB guest page is not necessarily backed by a 2 MB host page, and the mapping work at the hypervisor layer does not shrink just because your guest page is bigger. Second, ballooning and KSM interact badly with huge pages: KSM cannot deduplicate a 2 MB page that differs in one byte, so THP reduces the dedup ratio on hosts that rely on it. Third, memory overcommit on the host means a 2 MB page fault may trigger host-level reclaim \u2014 cost that appears to you as unexplained stall time with no local cause.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Practical consequence: on a VPS, the marginal throughput benefit of THP is smaller than on bare metal, while the tail-latency risk is similar or larger. That shifts the default toward <code>madvise<\/code>, and toward <code>never<\/code> for fsync-bound databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are choosing a plan for a latency-sensitive database, <a href=\"https:\/\/virtualserversvps.com\/\">the VPS configurations on virtualserversvps.com<\/a> show memory and storage per tier, which is the relevant input: THP tuning buys you a few percent, but undersized RAM forces you into the swap path where huge pages stop helping entirely.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A sane default and how to apply it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unless you have measured a benefit from <code>always<\/code>, run <code>madvise<\/code> with <code>defrag<\/code> set to <code>defer<\/code> so that compaction never runs synchronously in the faulting thread. Persist it with a systemd unit rather than a one-off <code>echo<\/code>, because sysfs settings do not survive reboots:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \/etc\/systemd\/system\/thp-tune.service\n[Unit]\nDescription=Set THP policy\nAfter=sysinit.target\n\n[Service]\nType=oneshot\nExecStart=\/bin\/sh -c 'echo madvise &gt; \/sys\/kernel\/mm\/transparent_hugepage\/enabled; echo defer &gt; \/sys\/kernel\/mm\/transparent_hugepage\/defrag'\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For a database that owns its own buffer pool and has already sized it to fit in RAM, <code>never<\/code> is defensible and simpler to reason about \u2014 the buffer pool does not need TLB coverage because it is accessed through a small number of hot pages. For a JVM or Go service with a large heap, <code>madvise<\/code> plus checking whether the runtime already issues <code>MADV_HUGEPAGE<\/code> is the better starting point.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then re-measure. THP is one of the few kernel knobs where the correct answer is site-specific rather than universal, and the p99.9 number from your own workload is the only evidence that should settle it.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>always vs madvise vs never, khugepaged collapse counters, and how to measure THP tail-latency effects on a virtualized guest with perf and vmstat.<\/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-1715","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 Huge Pages on a VPS: Measuring THP Effects on Latency and Memory - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"always vs madvise vs never, khugepaged collapse counters, and how to measure THP tail-latency effects on a virtualized guest with perf and vmstat.\" \/>\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\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory\" \/>\n<meta property=\"og:description\" content=\"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-15T10:40:03+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\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/\",\"name\":\"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-11-15T10:40:03+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"always vs madvise vs never, khugepaged collapse counters, and how to measure THP tail-latency effects on a virtualized guest with perf and vmstat.\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory\"}]},{\"@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 Huge Pages on a VPS: Measuring THP Effects on Latency and Memory - Virtual Servers VPS Blog","description":"always vs madvise vs never, khugepaged collapse counters, and how to measure THP tail-latency effects on a virtualized guest with perf and vmstat.","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\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/","og_locale":"en_US","og_type":"article","og_title":"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory","og_description":"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory","og_url":"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-11-15T10:40:03+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\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/","url":"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/","name":"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-11-15T10:40:03+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"always vs madvise vs never, khugepaged collapse counters, and how to measure THP tail-latency effects on a virtualized guest with perf and vmstat.","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/thp-madvise-khugepaged-transparent-huge-pages-vps-p99\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Transparent Huge Pages on a VPS: Measuring THP Effects on Latency and Memory"}]},{"@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\/1715","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=1715"}],"version-history":[{"count":2,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":1749,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1715\/revisions\/1749"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}