# Disabling Transparent Huge Pages on Ubuntu (Repost)

Check Ubuntu’s THP state and disable it temporarily or persistently using GRUB, systemd, or rc.local.

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:

```bash
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:

```bash
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:

```bash
sudo nano /etc/default/grub
```

2. Find `GRUB_CMDLINE_LINUX_DEFAULT` or `GRUB_CMDLINE_LINUX`. Add `transparent_hugepage=never` inside the quoted kernel arguments. For example:

```bash
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.

3. Save the file and update GRUB:

```bash
sudo update-grub
```

4. Reboot:

```bash
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`:

```ini
[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`:

```bash
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:

```bash
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:

```bash
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.