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
- Check current THP status
Runcat /sys/kernel/mm/transparent_hugepage/enabled. The output will show[always] madvise neveror similar. The bracketed option is currently active. - Temporarily disable THP (for testing)
Runecho never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled. This takes effect immediately but resets on reboot. Verify with the command from step 1 –nevershould be bracketed. - Make it permanent via kernel boot parameter (recommended)
Edit the GRUB configuration file:sudo nano /etc/default/grub. Find the line starting withGRUB_CMDLINE_LINUX_DEFAULTand addtransparent_hugepage=neverinside the quotes. For example:GRUB_CMDLINE_LINUX_DEFAULT="quiet splash transparent_hugepage=never" - 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.cfgon UEFI systems). - Reboot the system
Runsudo reboot. After reboot, verify with the command from step 1 – it should showalways madvise [never]. - 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.servicewith 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.serviceand start it:sudo systemctl start disable-thp.service. - Verify after reboot again
If you used the systemd service, reboot and check the status again. If it showsalways, the service may have run too early – adjustAfter=toAfter=sysinit.targetand try again.
Common Mistakes
- Forgetting to update GRUB after editing – The change in
/etc/default/grubdoes nothing until you runupdate-gruborgrub2-mkconfig. - Using the wrong path for GRUB config on UEFI – On UEFI systems, the output file is often
/boot/efi/EFI/redhat/grub.cfgor similar; using the BIOS path will not apply the change. - Only disabling THP for the current session – Running
echo neverwithout a persistent method will revert after reboot. - Editing the wrong file – Some guides suggest editing
/sys/kernel/mm/transparent_hugepage/enableddirectly; 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
enabledfile 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
madviseas a middle ground – If you only want to disable THP for specific applications, setmadviseand then usemadvise(MADV_HUGEPAGE)in your code ormadvisein/etc/rc.localfor specific processes. - Check other THP settings – The
defragfile also affects behavior; settingneverthere can further reduce latency spikes. - Document the change – Add a comment in
/etc/default/grubso future admins know why the parameter is there. - On systemd-boot systems – Edit
/etc/kernel/cmdlineand runsudo kernel-install add-allor usebootctl updatedepending on your distribution.
Troubleshooting
- THP is still enabled after reboot – Check if the boot parameter was actually applied:
cat /proc/cmdlineshould containtransparent_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/gruband re-runupdate-grub. - The
enabledfile 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 fortransparent_hugepagein/usr/lib/systemd/systemand 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
madviseinstead.
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.