Huge Pages

Normal pages are 4KB. When a process has gigabytes of memory, tracking millions of 4KB pages floods the TLB and slows everything down. Huge pages (2MB or 1GB) reduce the number of TLB entries needed, often delivering significant performance gains — but can also hurt if used wrong.

Why Huge Pages Help

What's the TLB and why does it matter? The TLB (Translation Lookaside Buffer) is a small hardware cache that stores recent virtual→physical address translations. It has limited entries (typically 64-1024). If a process has 8GB of memory in 4KB pages, that's 2 million pages — far more than the TLB can cache. TLB misses require expensive page table walks. With 2MB pages, 8GB needs only 4096 pages — far fewer TLB entries.

Performance improvement from huge pages: 5-30% for memory-intensive workloads (databases, HPC, ML training).

Explicit Huge Pages — HugeTLBFS

Reserve huge pages at system boot, then applications explicitly request them:

# Reserve 512 x 2MB huge pages = 1GB echo 512 > /proc/sys/vm/nr_hugepages # Persistent (in /etc/sysctl.conf): vm.nr_hugepages = 512 # Check allocation cat /proc/meminfo | grep Huge # HugePages_Total: 512 # HugePages_Free: 480 # HugePages_Rsvd: 32 # Hugepagesize: 2048 kB # Mount HugeTLBFS (applications use this) mount -t hugetlbfs none /mnt/huge

Used by: Oracle Database, PostgreSQL with huge_pages=on, Java with -XX:+UseHugeTLBFS.

Transparent Huge Pages (THP)

What is THP? THP is the kernel's attempt to use huge pages automatically, without application changes. The kernel monitors memory usage and tries to "collapse" adjacent 4KB pages into a single 2MB page when possible. No configuration required by the application.
# Check THP status cat /sys/kernel/mm/transparent_hugepage/enabled # [always] madvise never ← "always" is default on most distros # Options: # always = always try to use THP # madvise = only when application requests it (madvise MADV_HUGEPAGE) # never = disable completely # Change to madvise (recommended for mixed workloads): echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

When THP Hurts — Disable It for These

Why do Redis and MongoDB recommend disabling THP? THP collapsing is done by a background thread (khugepaged) that occasionally causes latency spikes of 100ms+ when it collapses or splits pages. Redis is a latency-sensitive in-memory database — a 100ms pause is catastrophic. The standard advice: disable THP on Redis, MongoDB, and similar latency-sensitive services.
# Disable THP (add to /etc/rc.local or systemd service) echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag # Verify cat /sys/kernel/mm/transparent_hugepage/enabled # always madvise [never] ← brackets show current setting

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.