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 viamadvise(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'ifteeis 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.
- Edit the GRUB configuration:
sudo nano /etc/default/grub - Find the
GRUB_CMDLINE_LINUXline and add the parametertransparent_hugepage=never(oralways,madvise). Example:
If the line already contains other parameters, separate them with spaces.GRUB_CMDLINE_LINUX="transparent_hugepage=never quiet splash" - Update the GRUB configuration:
- For Ubuntu/Debian:
sudo update-grub - For CentOS/RHEL/Fedora:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
- For Ubuntu/Debian:
- 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.
- Check THP status:
cat /sys/kernel/mm/transparent_hugepage/enabled
Ensure the expected mode is active. - Monitor memory usage:
- Use
free -hfor a general memory overview. - For detailed hugepage analysis:
Notecat /proc/meminfo | grep -i hugeHugePages_Total,HugePages_Free(these are for explicit hugepages, not THP), but THP can also affectAnonHugePagesin/proc/meminfo. - Tools like
vmstat 1(check columnssi,sofor swap activity,us,syfor CPU) orsar -r 1for history.
- Use
- Assess application performance:
- For databases: check query latency, operations per second.
- For general workloads: use
perforpidstatto analyze system calls and TLB misses. - If THP caused issues (e.g., high
pgfaultor latency), consider disabling or switching tomadvisemode.
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=madvisein/etc/sysctl.confand apply withsudo sysctl -p. - Via GRUB: remove or comment out the
transparent_hugepageparameter fromGRUB_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:
- The active THP mode with
cat /sys/kernel/mm/transparent_hugepage/enabled. - Persistence of settings after reboot (if permanent methods were applied).
- 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.
- For advanced checks, use
cat /proc/vmstat | grep -i hugefor 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. Usesudo teeas shown above. - Changes not applied after reboot:
- For sysctl: check syntax in
/etc/sysctl.conf(no typos), runsudo sysctl -pand check output for errors. - For GRUB: ensure the parameter is added to
GRUB_CMDLINE_LINUX, not other lines. Afterupdate-grub, verify the parameter appears in the boot menu (e.g., viacat /proc/cmdline).
- For sysctl: check syntax in
- THP remain enabled despite
neversetting: 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
madvisemode — 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_memoryshows active defragmentation, orvmstatshows highpgfault, consider disabling THP or usingmadvise. You can also setdefragtoneverordefer, 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 thetransparent_hugepagemodule is loaded (usually built into the kernel).