LinuxMedium

Transparent Hugepages: Management and Fine-Tuning in Linux

This guide will help you understand what Transparent Hugepages are, how to enable, disable, or configure them in Linux, and how they affect system performance.

Updated at February 16, 2026
15-30 minutes
Medium
FixPedia Team
Применимо к:Linux kernel 2.6.38 and aboveUbuntu 16.04+CentOS 7/RHEL 7+Debian 9+

Introduction

Transparent Hugepages (THP) is a Linux kernel feature that automatically manages huge pages (memory pages of 2MB or 1GB) without explicit allocation by the application. The goal of THP is to reduce the number of entries in the TLB (Translation Lookaside Buffer), lower the overhead of memory page handling, and improve overall performance, especially for memory-intensive applications such as database management systems (PostgreSQL, MySQL), virtual machines, or in-memory data processing systems.

However, THP are not always optimal: in some scenarios they can cause memory fragmentation, increase latency, or conflict with NUMA optimizations. Therefore, managing THP is an important task for system administrators and DevOps engineers. This guide explains in detail how to check the current THP status, temporarily or permanently change their mode, and how to monitor the impact on the system.

Prerequisites

Before you begin, ensure your system meets the following conditions:

  • Operating system: Linux with kernel version 2.6.38 or higher (THP were introduced in 2.6.38, most modern distributions support them).
  • Permissions: root or sudo access is required to modify system files and configurations.
  • Tools: access to the command line (terminal), basic knowledge of Linux commands (cat, echo, nano/vim), as well as understanding of sysctl and GRUB.
  • Recommendations: before making changes in a production environment, it is recommended to test on a staging server or for a specific application to assess the impact on performance.

Step 1: Check Current Transparent Hugepage Status

First, you need to determine in which mode THP are operating on your system. The Linux kernel provides three modes:

  • always — THP are always attempted for new memory areas.
  • madvise — THP are allocated only for areas explicitly marked by the application via madvise(MADV_HUGEPAGE).
  • never — THP are completely disabled.

Run the command to view the active mode:

cat /sys/kernel/mm/transparent_hugepage/enabled

Typical output:

always madvise [never]

Square brackets indicate the currently active mode — in this case never. If always is active, the output will be [always] madvise never.

Also check the memory defragmentation settings, which affect the aggressiveness of THP:

cat /sys/kernel/mm/transparent_hugepage/defrag

Output may include values always, madvise, never, or defer. Defragmentation can add overhead, so in production madvise or never are often used.

Step 2: Temporarily Change THP Mode

For quick testing or temporary disabling of THP (effective until reboot), write the desired value to the corresponding sysfs file. For example, to disable THP:

echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

If you want to temporarily enable THP in always mode:

echo always | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

⚠️ Important: Some systems may require using sudo sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' if tee is unavailable. Ensure the command is executed with root privileges.

After changing, check the status again (as in Step 1) to confirm the mode switch. This is useful for quick diagnostics: if a performance issue disappears after disabling THP, they may have been the cause.

Step 3: Permanent Configuration via sysctl

To make changes persist after reboot, use the sysctl mechanism. Open the sysctl configuration file:

sudo nano /etc/sysctl.conf

Add a line (to disable THP):

vm.transparent_hugepage=never

For madvise mode (often recommended as a balance):

vm.transparent_hugepage=madvise

Save the file (Ctrl+O, Enter, Ctrl+X in nano) and apply the settings without rebooting:

sudo sysctl -p

The command will reread /etc/sysctl.conf and apply the parameters. Check the THP status — the setting should now persist after the next reboot.

💡 Tip: If you use a separate file in /etc/sysctl.d/ (e.g., /etc/sysctl.d/99-thp.conf), this allows easy management of settings without editing the main file.

Step 4: Configuration via Kernel Parameters (GRUB)

An alternative (and often more reliable for some distributions) method is to pass a kernel parameter via the GRUB bootloader. This is especially useful if sysctl settings are overridden by other mechanisms.

  1. Edit the GRUB configuration:
    sudo nano /etc/default/grub
    
  2. Find the GRUB_CMDLINE_LINUX line and add the parameter transparent_hugepage=never (or always, madvise). Example:
    GRUB_CMDLINE_LINUX="transparent_hugepage=never quiet splash"
    
    If the line already contains other parameters, separate them with spaces.
  3. Update the GRUB configuration:
    • For Ubuntu/Debian:
      sudo update-grub
      
    • For CentOS/RHEL/Fedora:
      sudo grub2-mkconfig -o /boot/grub2/grub.cfg
      
  4. Reboot the system:
    sudo reboot
    

After reboot, check the THP status — the kernel parameter should take precedence.

Step 5: Verification and Monitoring After Changes

After applying settings (especially via GRUB, which requires a reboot), it's important to ensure changes took effect and assess their impact on performance.

  1. Check THP status:
    cat /sys/kernel/mm/transparent_hugepage/enabled
    

    Ensure the expected mode is active.
  2. Monitor memory usage:
    • Use free -h for a general memory overview.
    • For detailed hugepage analysis:
      cat /proc/meminfo | grep -i huge
      
      Note HugePages_Total, HugePages_Free (these are for explicit hugepages, not THP), but THP can also affect AnonHugePages in /proc/meminfo.
    • Tools like vmstat 1 (check columns si, so for swap activity, us, sy for CPU) or sar -r 1 for history.
  3. Assess application performance:
    • For databases: check query latency, operations per second.
    • For general workloads: use perf or pidstat to analyze system calls and TLB misses.
    • If THP caused issues (e.g., high pgfault or latency), consider disabling or switching to madvise mode.

Step 6: Revert to Default Settings

If you decide that THP are better managed automatically (the madvise mode is often the default in modern distributions), do the following:

  • Via sysctl: set vm.transparent_hugepage=madvise in /etc/sysctl.conf and apply with sudo sysctl -p.
  • Via GRUB: remove or comment out the transparent_hugepage parameter from GRUB_CMDLINE_LINUX, update GRUB, and reboot.
  • Temporarily (until reboot):
    echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
    

After reverting to madvise, THP will be allocated only for memory areas explicitly requested by the application via madvise, reducing the risk of fragmentation but potentially limiting benefits for unprepared applications.

Verification

After any changes (temporary or permanent), be sure to check:

  1. The active THP mode with cat /sys/kernel/mm/transparent_hugepage/enabled.
  2. Persistence of settings after reboot (if permanent methods were applied).
  3. System stability and performance of key applications. If you observe performance degradation, increased I/O wait, or memory errors, THP may require different configuration or complete disabling.
  4. For advanced checks, use cat /proc/vmstat | grep -i huge for huge page fault statistics.

Troubleshooting

  • "Permission denied" error when writing to /sys/kernel/mm/transparent_hugepage/enabled: Ensure the command is run with sudo or as root. Use sudo tee as shown above.
  • Changes not applied after reboot:
    • For sysctl: check syntax in /etc/sysctl.conf (no typos), run sudo sysctl -p and check output for errors.
    • For GRUB: ensure the parameter is added to GRUB_CMDLINE_LINUX, not other lines. After update-grub, verify the parameter appears in the boot menu (e.g., via cat /proc/cmdline).
  • THP remain enabled despite never setting: Some distributions (especially those with patches for cloud environments) may force-enable THP. Check your distribution's documentation. Also ensure no other configurations (e.g., in systemd or init scripts) override the settings.
  • Performance drop after disabling THP: This is possible if the application was optimized for huge pages. Test with madvise mode — it often provides a balance. For specific applications (e.g., Oracle DB), consult vendor documentation on huge page configuration.
  • High memory fragmentation with THP enabled: If cat /proc/sys/vm/compact_memory shows active defragmentation, or vmstat shows high pgfault, consider disabling THP or using madvise. You can also set defrag to never or defer, but this may reduce THP effectiveness.
  • Incorrect output in /sys/kernel/mm/transparent_hugepage/enabled: In rare cases the file may be unavailable (e.g., in minimalist containers). Ensure the kernel supports THP (kernel >= 2.6.38) and that the transparent_hugepage module is loaded (usually built into the kernel).

F.A.Q.

What are Transparent Hugepages and why are they needed?
How to check the current status of Transparent Hugepages?
Can enabling Transparent Hugepages cause issues?
How to disable Transparent Hugepages permanently?

Hints

Check current THP status
Temporarily change THP mode
Permanent configuration via sysctl
Configuration via kernel parameters (GRUB)
Verification and monitoring after changes
Revert to default settings
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community