Skip to content

Another Boring Tech Blog

Menu
  • Start
  • About Me
Menu

How to Permanently Disable Transparent Huge Pages (THP) on Ubuntu 22.04

Posted on February 23, 2024November 25, 2024 by Vinicius Grippa

Disabling Transparent Huge Pages (THP) permanently on Ubuntu 22.04 requires editing system configuration files to apply the change at boot time. There are a few methods to achieve this, but one common approach is to use rc.local or a custom systemd service since Ubuntu may not have rc.local enabled by default in newer versions. Here, I’ll show you how to create a systemd service to disable THP.

Step 1: Create a systemd Service File

  1. Use your favorite text editor to create a new systemd service file. Here, we’ll use vi:
    1
    sudo vi /etc/systemd/system/disable-thp.service
  2. Add the following content to the file:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [Unit]
    Description=Disable Transparent Huge Pages
     
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'
    ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'
     
    [Install]
    WantedBy=multi-user.target
  3. Save and close the file.

Step 2: Reload Systemd and Enable the Service

After creating the service file, you need to reload the systemd manager configuration, enable the service to start at boot, and then start the service immediately to apply the change without rebooting.

Execute the following commands in the terminal:

1
2
3
sudo systemctl daemon-reload
sudo systemctl enable disable-thp.service
sudo systemctl start disable-thp.service

Step 3: Verify the Changes

To verify that Transparent Huge Pages have been disabled, you can use the cat command to check the current settings:

1
2
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

Both commands should output never, indicating that Transparent Huge Pages are disabled.

Conclusion

By following these steps, you’ve created a systemd service to disable Transparent Huge Pages permanently on Ubuntu 22.04. This change will persist across reboots, ensuring that THP is disabled each time the system starts. This method is preferred for systems where THP may interfere with the performance of certain applications, especially databases like MongoDB, Redis, etc., that recommend disabling THP for better performance and efficiency.

Post navigation

← How to Create SSH Keys
Introduction to the New Percona Everest Beta →
© 2025 Another Boring Tech Blog | Powered by Minimalist Blog WordPress Theme