449 words
2 minutes

Disabling Transparent Huge Pages on Ubuntu (Repost)

2025-09-23
Views —

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:

Terminal window
cat /sys/kernel/mm/transparent_hugepage/enabled

The 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:

Terminal window
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo 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.

  1. Edit the configuration file:
Terminal window
sudo nano /etc/default/grub
  1. Find GRUB_CMDLINE_LINUX_DEFAULT or GRUB_CMDLINE_LINUX. Add transparent_hugepage=never inside the quoted kernel arguments. For example:
Terminal window
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.

  1. Save the file and update GRUB:
Terminal window
sudo update-grub
  1. Reboot:
Terminal window
sudo reboot

Option 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=oneshot
ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled && echo never > /sys/kernel/mm/transparent_hugepage/defrag'
[Install]
WantedBy=basic.target

Alternatively, if your system supports rc.local, edit or create /etc/rc.local and insert the following before exit 0:

Terminal window
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

Make sure rc.local is executable.

If you chose the systemd approach, reload the configuration, enable the service, and start it:

Terminal window
sudo systemctl daemon-reload
sudo systemctl enable disable-thp.service
sudo systemctl start disable-thp.service

Reboot to confirm that the configuration persists.

4. Verify the setting#

After rebooting, run the check again:

Terminal window
cat /sys/kernel/mm/transparent_hugepage/enabled

The 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 enabled and defrag to never.
  • NUMA is separate too. Follow the requirements for your particular software rather than automatically combining numa=off with 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.

Disabling Transparent Huge Pages on Ubuntu (Repost)
https://blog.coding1024.com/en/posts/4827ed55/
Author
huangjx
Published at
2025-09-23
License
CC BY-NC-SA 4.0

Comments

Questions, ideas and experiences are welcome. Please be kind and avoid sharing private information.

Loading comments…