Virtualization

HugeTLB vs Transparent Huge Pages: A Practical Guide for Databases

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Database tuning guides love to mention huge pages, and they often give contradictory advice: enable them for PostgreSQL, disable them for Redis, and it depends for MongoDB. The confusion comes from the fact that Linux has two very different huge page mechanisms, HugeTLB and Transparent Huge Pages (THP), which share a name but behave in opposite ways. This article explains why huge pages exist, how each mechanism works, and how to configure them sensibly on a Linux VPS.

Why page size matters: the TLB

Programs use virtual addresses. The CPU translates them to physical addresses by walking page tables, a multi-level tree in memory. On x86-64 with 4 KB pages, a translation can require four or five memory lookups. To avoid doing this on every access, the CPU caches recent translations in the Translation Lookaside Buffer (TLB).

The TLB is small, typically a few thousand entries across its levels. With 4 KB pages, 1,536 entries cover only 6 MB of memory. A database with a 16 GB buffer pool will miss the TLB constantly, and each miss costs a page walk. In a VM, it is worse: with nested paging (Intel EPT, AMD NPT), a TLB miss walks both the guest and the host page tables, which can multiply the cost.

Huge pages fix this by making each TLB entry cover more memory. On x86-64 the sizes are 2 MB and 1 GB. With 2 MB pages, the same 1,536 entries cover 3 GB. Fewer misses, shorter page walks, and smaller page tables.

HugeTLB: explicit, reserved huge pages

HugeTLB (often called hugetlbfs or static huge pages) is the older mechanism. The administrator reserves a pool of huge pages, and applications request them explicitly through mmap with MAP_HUGETLB, shmget with SHM_HUGETLB, or by mapping files on a hugetlbfs mount.

Key properties:

  • Reserved up front. Pages in the pool are removed from general use, even if no application uses them yet.
  • Never swapped and never split. They stay in RAM as huge pages.
  • Predictable. No background compaction or collapsing happens at runtime.
  • Opt-in. Only applications that ask for them get them.

Reserve them through sysctl:

sudo sysctl -w vm.nr_hugepages=1024     # 1024 x 2 MB = 2 GB
grep -i huge /proc/meminfo

The main downside is rigidity: you must size the pool correctly, and reserved memory is wasted if unused. Allocating a large pool late in uptime may also fail if memory is fragmented, so reserve it early, ideally at boot.

Transparent Huge Pages: automatic, but with side effects

THP was added so that applications get huge pages without code changes. The kernel tries to back anonymous memory with 2 MB pages at fault time, and a background thread, khugepaged, scans memory and collapses runs of 4 KB pages into huge pages.

The behavior is controlled in sysfs:

cat /sys/kernel/mm/transparent_hugepage/enabled
# [always] madvise never

cat /sys/kernel/mm/transparent_hugepage/defrag
# always defer defer+madvise [madvise] never
  • always: THP for all eligible memory.
  • madvise: only for regions where the application called madvise(MADV_HUGEPAGE).
  • never: disabled.

The defrag setting decides whether a page fault may stall while the kernel compacts memory to create a huge page. Direct compaction is the source of most THP latency complaints.

THP's problems for databases come from a few places: latency spikes from compaction, memory bloat when a 2 MB page is allocated for a process that touches only a few KB of it, and copy-on-write costs after fork(), where a single write copies 2 MB instead of 4 KB.

Database-specific advice

PostgreSQL

PostgreSQL benefits from huge pages for shared_buffers, but through HugeTLB, not THP. Set huge_pages = try (default) or on in postgresql.conf, and reserve enough pages. On PostgreSQL 15 and later you can ask the server how many it needs:

sudo -u postgres postgres -D /var/lib/postgresql/16/main -C shared_memory_size_in_huge_pages

Set vm.nr_hugepages to that value or slightly higher. Setting THP to madvise or never is a sensible companion.

Redis

Redis forks to write RDB snapshots and rewrite AOF files. With THP enabled, copy-on-write during that fork becomes much more expensive, causing latency spikes and memory growth. Redis logs a warning at startup when THP is always. Set it to never or madvise.

MongoDB

The advice changed over time. MongoDB versions before 8.0 recommend disabling THP. MongoDB 8.0 and later ship an upgraded TCMalloc and recommend enabling THP. Check the production notes for the exact version you run.

What this means for your VPS

First, check your current state:

cat /sys/kernel/mm/transparent_hugepage/enabled
grep -E 'AnonHugePages|HugePages_(Total|Free)|Hugepagesize' /proc/meminfo

To switch THP to madvise now:

echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo defer+madvise | sudo tee /sys/kernel/mm/transparent_hugepage/defrag

These settings reset on reboot. Make them permanent with the kernel parameter transparent_hugepage=madvise in GRUB_CMDLINE_LINUX (then run update-grub or grub2-mkconfig), or with a small systemd oneshot unit that writes the values at boot. Persist vm.nr_hugepages in /etc/sysctl.d/90-hugepages.conf.

Remember that HugeTLB pages are reserved: they count as used memory and cannot be reclaimed by a memory balloon, which we cover in our memory ballooning guide. Size the pool for the plan you actually have on /pricing, and leave room for the OS, connections, and page cache.

FAQ

Should I just disable Transparent Huge Pages everywhere?

Not necessarily. madvise is a good default for servers: applications that benefit can request huge pages, while everything else keeps 4 KB pages. Disable THP entirely only when a workload such as Redis or an older MongoDB version recommends it.

Can I use 1 GB huge pages on a VPS?

Only if the virtual CPU exposes the pdpe1gb flag, which you can check in /proc/cpuinfo. Even then, 1 GB pages are usually reserved at boot with kernel parameters such as hugepagesz=1G hugepages=4, and they suit large, dedicated database servers more than small instances.

How do I know if huge pages are actually being used?

For HugeTLB, compare HugePages_Total with HugePages_Free in /proc/meminfo; the difference is in use, and HugePages_Rsvd shows pages an application has claimed but not touched yet. For THP, AnonHugePages shows how much anonymous memory is currently backed by huge pages.

Related guides