Disabling Transparent Huge Pages on Ubuntu (Repost)
There are two main ways to disable Transparent Huge Pages (THP) on Ubuntu: temporarily, until the next reboot, or persistently. This repost walks through both methods and the checks to make afterward.
1. Check the current THP setting
Open a terminal and run:
cat /sys/kernel/mm/transparent_hugepage/enabledThe value in square brackets is selected. [always] enables THP generally, while [madvise] permits it for applications that request it. An output of always madvise [never] indicates that THP is disabled.
2. Disable THP temporarily
To turn it off immediately for testing:
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabledecho never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag # 建议同时也关闭碎片整理The second command also disables THP defragmentation. These settings apply only to the current session; they do not persist across a reboot.
3. Disable THP persistently
Option A: Edit the GRUB configuration
This guide uses GRUB as the main approach.
- Edit the configuration file:
sudo nano /etc/default/grub- Find
GRUB_CMDLINE_LINUX_DEFAULTorGRUB_CMDLINE_LINUX. Addtransparent_hugepage=neverinside the quoted kernel arguments. For example:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash transparent_hugepage=never"# 或者GRUB_CMDLINE_LINUX="transparent_hugepage=never"If your particular database deployment also calls for disabling NUMA, its separate argument is numa=off. Do not treat that as an inherent requirement of disabling THP.
- Save the file and update GRUB:
sudo update-grub- Reboot:
sudo rebootOption B: Use a systemd service or rc.local
The original guide presents this as an alternative for deployments that manage the setting through a startup service, mentioning MongoDB as an example.
Create a unit such as /etc/systemd/system/disable-thp.service:
[Unit]Description=Disable Transparent Huge Pages (THP)After=sysinit.target
[Service]Type=oneshotExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled && echo never > /sys/kernel/mm/transparent_hugepage/defrag'
[Install]WantedBy=basic.targetAlternatively, if your system supports rc.local, edit or create /etc/rc.local and insert the following before exit 0:
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabledfiif test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defragfiMake sure rc.local is executable.
If you chose the systemd approach, reload the configuration, enable the service, and start it:
sudo systemctl daemon-reloadsudo systemctl enable disable-thp.servicesudo systemctl start disable-thp.serviceReboot to confirm that the configuration persists.
4. Verify the setting
After rebooting, run the check again:
cat /sys/kernel/mm/transparent_hugepage/enabledThe expected result is always madvise [never].
Considerations
- Why disable THP? The original repost discusses workloads involving Oracle Database, MongoDB, Redis, and Hadoop where THP can cause unwanted latency, overhead, or performance issues. Requirements depend on the application and version.
- Performance is a tradeoff. THP can benefit other workloads. If you have no specific problem or application requirement, disabling it is not automatically an improvement.
- Defragmentation is separate. The runtime examples set both
enabledanddefragtonever. - NUMA is separate too. Follow the requirements for your particular software rather than automatically combining
numa=offwith the THP change.
Whichever method you choose, reboot and verify the resulting state. If something does not behave as expected, investigate the requirements of the particular application before changing further memory settings.
Comments
Questions, ideas and experiences are welcome. Please be kind and avoid sharing private information.
Loading comments…