Validate anonymous THP mappings and boot-time ordering
When Redis shows latency spikes during background saves or rewrites without a noticeable change in total memory usage, immediately disabling transparent huge pages risks confusing correlation with causation. A valid change must answer three questions: what does the kernel currently allow, what does the target process actually use, and does latency improve under the same load? This article uses Ubuntu Server 24.04 LTS as an example and addresses only the THP policy for ordinary anonymous memory. It does not change database capacity or persistence settings.
1. Define the scope of anonymous THP checks
Transparent huge pages organize contiguous memory into larger pages, potentially reducing address translation overhead. However, allocation, compaction, and copy-on-write costs can also affect latency-sensitive workloads. Disabling THP is therefore an experiment to validate, not a universal baseline for every dedicated server.
THP is not the same as explicitly reserved HugeTLB pages or shared-memory huge pages. The enabled and defrag checks in this article do not replace checks of shared-memory policy. Newer kernels may also expose per-page-size settings. If you find additional directories, record their actual modes rather than treating a single switch as proof that every huge-page mechanism is disabled.
A successful sysfs write proves only that the policy change was accepted. It does not prove that existing mappings have been split, much less that application latency has decreased.
Before starting, record the application version, kernel version, request concurrency, dataset size, and background task status. For Runner machines, retrieve the credentials from the console after payment and use SSH for Linux. If multiple addresses are listed, connect to the first one. The checks below do not depend on a particular memory tier.
2. Establish a baseline for both policy and process
First, save the original output. Square brackets identify the current mode; they do not mean that every option on the line is active at once. Run the commands in the same administrative session and retain the files until validation is complete.
THP=/sys/kernel/mm/transparent_hugepage
BASE=$(mktemp -d /tmp/thp-baseline.XXXXXX)
uname -r > "$BASE/kernel"
cat "$THP/enabled" | tee "$BASE/enabled"
cat "$THP/defrag" | tee "$BASE/defrag"
grep -E '^(AnonHugePages|ShmemHugePages|HugePages_Total):' /proc/meminfo
If the directory does not exist, first check whether the running kernel supports the feature. Do not create ordinary files with the same names. always, madvise, and never mean broad enablement, allocation primarily through application hints, and disabling the anonymous THP controlled by this setting, respectively. defrag controls the corresponding memory compaction behavior; the two settings are not interchangeable.
Find the actual process through its service
The following example uses redis-server.service from the system package. For an instance-specific service, container, or custom deployment, substitute the actual unit. If MainPID is zero, stop the check rather than continuing with an incorrect path.
PID=$(systemctl show redis-server.service --property=MainPID --value)
test "$PID" -gt 0
sudo awk '/^(Rss|Pss|Anonymous|AnonHugePages):/' \
"/proc/$PID/smaps_rollup"
sudo awk '/^(AnonHugePages|THPeligible|VmFlags):/' \
"/proc/$PID/smaps"
smaps_rollup provides process-wide totals, while smaps lets you inspect individual mappings. THPeligible indicates whether a mapping is eligible, not whether it has already received huge pages. Likewise, the machine-wide AnonHugePages value cannot be attributed to a single Redis instance. If permissions are insufficient, read the files through an authorized administrative account rather than relaxing access restrictions across /proc.
3. Make a temporary change while keeping test conditions consistent
First protect the data and confirm the recovery procedure, then change only THP. Do not adjust the Redis memory limit, persistence parameters, or worker threads at the same time, or you will not be able to attribute the results to this change.
printf '%s\n' never | sudo tee "$THP/enabled"
printf '%s\n' never | sudo tee "$THP/defrag"
cat "$THP/enabled"
cat "$THP/defrag"
This change does not guarantee that existing anonymous huge pages will split immediately. Read the target process mappings again. If huge pages remain and the experiment requires fresh mappings, first drain requests, then perform a controlled application restart using the established operating procedure. Do not deliberately create memory pressure on a production machine just to obtain a zero value.
Retesting should use the same warm-up phase and cover a comparable dataset, request distribution, and background-save state. At a minimum, record application latency percentiles, throughput, CPU usage, available memory, and paging activity. vmstat 1 10 can show short-term changes, but it is not a substitute for recording a full application workload cycle.
If the application version itself adjusts THP behavior, record that as well. When the policy changes but the process mappings do not, first explain where those mappings came from rather than immediately declaring the optimization effective.
4. Apply the policy before the application starts
Temporary writes generally do not survive a reboot. Once the change has been validated, you can create a separate systemd oneshot service to apply the policy before the target application starts. The following example still assumes that the target unit is indeed redis-server.service.
sudo tee /etc/systemd/system/anonymous-thp-policy.service >/dev/null <<'EOF'
[Unit]
Description=Apply anonymous THP policy
ConditionPathExists=/sys/kernel/mm/transparent_hugepage/enabled
Before=redis-server.service
[Service]
Type=oneshot
ExecStart=/bin/sh -ec 'echo never > /sys/kernel/mm/transparent_hugepage/enabled; echo never > /sys/kernel/mm/transparent_hugepage/defrag'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemd-analyze verify /etc/systemd/system/anonymous-thp-policy.service
sudo systemctl daemon-reload
sudo systemctl enable --now anonymous-thp-policy.service
Before= establishes ordering only; it does not start another service. Here, enabling the policy unit adds it to the normal boot transaction. Restarting Redis on its own also does not automatically rerun a policy unit that has already completed. If another tool later overwrites the policy, identify it and establish a single owner for the configuration instead of layering on more startup scripts.
systemctl show redis-server.service --property=After
systemctl status anonymous-thp-policy.service --no-pager
journalctl -b -u anonymous-thp-policy.service --no-pager
When recovery is possible, schedule a full machine reboot to verify the setup, then reread sysfs and the mappings for the new PID. A successful service status without evidence of the policy and mappings still leaves validation incomplete.
5. Complete validation with rollback results
Keep the conclusion within the scope of the test: what effect did the anonymous THP policy change have with this kernel, application version, and specified workload? A single low-latency sample is no substitute for repeated testing. If the benefit is inconsistent, retain the original configuration rather than continuing to stack unverified parameters.
To roll back, first disable and remove the added startup unit, then restore the two active modes recorded in the baseline. The commands below depend on the BASE and THP variables retained from earlier steps. In a new session, first specify the actual directory containing the baseline records.
sudo systemctl disable --now anonymous-thp-policy.service
sudo rm /etc/systemd/system/anonymous-thp-policy.service
sudo systemctl daemon-reload
for item in enabled defrag; do
mode=$(sed -n 's/.*\[\([^]]*\)\].*/\1/p' "$BASE/$item")
test -n "$mode" || break
printf '%s\n' "$mode" | sudo tee "$THP/$item"
done
Stopping the oneshot service does not itself restore the kernel policy, so you must explicitly write back the original values. After restoring them, read the output again and, if necessary, perform a controlled application restart and recheck the mappings. Finally, archive the baseline, policy changes, process evidence, workload conditions, and rollback results. This produces a reproducible verification record rather than leaving only the conclusion “THP is disabled.”
Frequently asked questions
Does setting THP to never immediately remove existing huge pages?
No. A policy change does not immediately split existing mappings. Check the target process’s AnonHugePages and, if mappings must be recreated, restart the application only after protecting its data and draining traffic.
Does zero AnonHugePages prove that latency is fixed?
No. It only describes anonymous huge page usage at the sampling time. Retest latency, CPU usage, and memory pressure with matching request distributions, concurrency, and background jobs.
Runner EPYC 7543P Dedicated Server
From $450/month