ThisCalculator

Everyday

Disable Transparent Huge Pages on Linux Permanently

Updated · Standard formula, shown below

Disable Transparent Huge Pages on Linux Permanently

Transparent Huge Pages (THP) can cause performance issues for databases, virtual machines, and latency-sensitive applications due to memory allocation delays and fragmentation. This guide shows you how to disable THP permanently on most modern Linux distributions, ensuring the setting survives reboots.

What You’ll Need

  • Root or sudo access to the system
  • A text editor (vi, nano, or similar)
  • Knowledge of your boot loader (GRUB2 is assumed, but systemd-boot is covered)

Step-by-Step

  1. Check current THP status
    Run cat /sys/kernel/mm/transparent_hugepage/enabled. The output will show [always] madvise never or similar. The bracketed option is currently active.
  2. Temporarily disable THP (for testing)
    Run echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled. This takes effect immediately but resets on reboot. Verify with the command from step 1 – never should be bracketed.
  3. Make it permanent via kernel boot parameter (recommended)
    Edit the GRUB configuration file: sudo nano /etc/default/grub. Find the line starting with GRUB_CMDLINE_LINUX_DEFAULT and add transparent_hugepage=never inside the quotes. For example:
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash transparent_hugepage=never"
  4. Update GRUB configuration
    On Debian/Ubuntu: sudo update-grub. On RHEL/CentOS/Fedora: sudo grub2-mkconfig -o /boot/grub2/grub.cfg (or /boot/efi/EFI/redhat/grub.cfg on UEFI systems).
  5. Reboot the system
    Run sudo reboot. After reboot, verify with the command from step 1 – it should show always madvise [never].
  6. Alternative: using a systemd service (if boot parameter is not possible)
    If you cannot modify kernel parameters (e.g., on a managed kernel), create a service file: sudo nano /etc/systemd/system/disable-thp.service with the following content:
    # Disable Transparent Huge Pages
    [Unit]
    Description=Disable Transparent Huge Pages
    DefaultDependencies=no
    After=sysinit.target
    Before=basic.target
    
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'
    RemainAfterExit=yes
    
    [Install]
    WantedBy=basic.target

    Then enable it: sudo systemctl enable disable-thp.service and start it: sudo systemctl start disable-thp.service.

  7. Verify after reboot again
    If you used the systemd service, reboot and check the status again. If it shows always, the service may have run too early – adjust After= to After=sysinit.target and try again.

Common Mistakes

  • Forgetting to update GRUB after editing – The change in /etc/default/grub does nothing until you run update-grub or grub2-mkconfig.
  • Using the wrong path for GRUB config on UEFI – On UEFI systems, the output file is often /boot/efi/EFI/redhat/grub.cfg or similar; using the BIOS path will not apply the change.
  • Only disabling THP for the current session – Running echo never without a persistent method will revert after reboot.
  • Editing the wrong file – Some guides suggest editing /sys/kernel/mm/transparent_hugepage/enabled directly; that is a virtual file and changes are lost on reboot.
  • Not checking if THP is actually supported – Some kernels or VMs may not have THP enabled; the enabled file might not exist, and the steps will fail.

Tips

  • Test before committing – Use the temporary disable method first to confirm your application works better with THP off.
  • Use madvise as a middle ground – If you only want to disable THP for specific applications, set madvise and then use madvise(MADV_HUGEPAGE) in your code or madvise in /etc/rc.local for specific processes.
  • Check other THP settings – The defrag file also affects behavior; setting never there can further reduce latency spikes.
  • Document the change – Add a comment in /etc/default/grub so future admins know why the parameter is there.
  • On systemd-boot systems – Edit /etc/kernel/cmdline and run sudo kernel-install add-all or use bootctl update depending on your distribution.

Troubleshooting

  • THP is still enabled after reboot – Check if the boot parameter was actually applied: cat /proc/cmdline should contain transparent_hugepage=never. If not, you may have edited the wrong file or forgot to update GRUB.
  • System fails to boot after adding the parameter – This is rare, but if it happens, boot into recovery mode or use a live CD to remove the parameter from /etc/default/grub and re-run update-grub.
  • The enabled file does not exist – Your kernel may not support THP (e.g., some minimal kernels). You can skip this guide; THP is already effectively disabled.
  • The systemd service does not work – Check the service status: systemctl status disable-thp.service. If it fails with “Permission denied”, ensure the service runs as root (it does by default). If it runs but the value resets later, another service may be re-enabling THP – search for transparent_hugepage in /usr/lib/systemd/system and mask those services.
  • Performance gets worse after disabling – Some workloads benefit from THP. Re-enable by removing the boot parameter and updating GRUB, or set madvise instead.

Frequently Asked Questions

What exactly are Transparent Huge Pages?

THP is a Linux memory management feature that automatically uses larger memory pages (2MB instead of 4KB) to reduce TLB misses. It works transparently to applications, but can cause latency spikes due to memory compaction.

Why would I want to disable them?

Databases (like MongoDB, PostgreSQL), Java applications, and real-time systems often suffer from THP-induced latency. Disabling THP provides more predictable memory allocation times.

Is it safe to disable THP?

Yes, for most systems. The kernel falls back to using regular 4KB pages, which is the standard behavior. The main trade-off is a slight increase in TLB misses, which is usually negligible compared to the latency improvements.

Can I disable THP only for specific applications?

Yes, by setting the enabled file to madvise and then having applications opt-in using the madvise() system call with MADV_HUGEPAGE. This is more complex but offers fine-grained control.

Does disabling THP affect memory usage?

It can slightly increase memory usage because 4KB pages have more overhead than 2MB pages, but the difference is typically less than 1% for most workloads.

Will the setting survive kernel updates?

Yes, the boot parameter is stored in GRUB configuration, which persists across kernel updates. The systemd service method also survives since it’s a system service.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.