Transparent Huge Pages (THP) consolidate 4 KB pages into 2 MB pages to reduce TLB pressure and improve memory access efficiency. This topic covers THP configuration options, defragmentation settings, and tuning recommendations for Alibaba Cloud Linux.
THP configurations
In Alibaba Cloud Linux 2, kernel 4.19.81-17.2 and earlier defaulted transparent_hugepage/enabled to madvise (selective THP). Starting from kernel 4.19.91-18, the default changed to always for compatibility with Red Hat Enterprise Linux 7, CentOS 7, and Amazon Linux 2. The always setting enables THP system-wide.
Global configuration
Configure THP through /sys/kernel/mm/transparent_hugepage/enabled:
-
alwaysTHP is globally enabled.
-
neverTHP is globally disabled.
-
madviseTHP is enabled only for memory regions marked with
MADV_HUGEPAGEviamadvise().NoteThe
MADV_HUGEPAGEflag signals to the kernel that an application intends to use huge pages for a specific memory region.
Defragmentation configuration
Beyond global THP settings, two defragmentation mechanisms also apply:
-
THP defragmentation: Merges scattered small pages into huge pages to reduce memory fragmentation.
-
khugepaged defragmentation: The
khugepageddaemon periodically scans memory and merges scattered small pages into contiguous huge pages.
The khugepaged daemon proactively merges pages in the background. The THP defrag setting controls allocation-time defragmentation strategy.
THP defragmentation
Controls memory behavior during page faults (Direct Reclaim, Background Reclaim, Direct Compaction, Background Compaction). Configure by writing to /sys/kernel/mm/transparent_hugepage/defrag:
-
alwaysThe system pauses the requesting process and performs synchronous Direct Reclaim and Direct Compaction. If enough contiguous memory becomes available, the THP is allocated.
-
deferFalls back to a 4 KB page and wakes
kswapd(Background Reclaim) andkcompactd(Background Compaction). Later,khugepagedmerges the 4 KB pages into a 2 MB THP when contiguous memory is available. -
madviseFor memory regions marked with
MADV_HUGEPAGEviamadvise(), behaves likealways. All other regions fall back to 4 KB pages.NoteIn Alibaba Cloud Linux 2 with kernel version 4.19.81-17.2 and later, the system default for this setting is
madvise. -
defer+madviseFor
MADV_HUGEPAGEregions (viamadvise()), behaves likealways. For all other regions, behaves likedefer. -
never
Disables allocation-time defragmentation.
khugepaged defragmentation
Key khugepaged configurations:
-
Enable or disable the feature
Configuration file:
/sys/kernel/mm/transparent_hugepage/khugepaged/defrag. Options:-
0Disables
khugepageddefragmentation. -
1When set to
1, thekhugepageddaemon periodically merges scattered 4 KB pages into 2 MB THPs during idle times.Note-
In Alibaba Cloud Linux 2 with kernel version 4.19.91-18 and later, the system default is
1. -
This acquires memory path locks. If
khugepagedscans at suboptimal times, it can degrade application performance.
-
-
-
Retry interval
Time
khugepagedwaits before retrying after a failed THP allocation. Default:60000ms (60 seconds). Configuration file:/sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs. -
Scan Interval
Time between
khugepagedwakeups. Default:10000ms (10 seconds). Configuration file:/sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs. -
Number of pages to scan
Pages scanned per
khugepagedwakeup. Default: 4096. Configuration file:/sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan.
THP configuration recommendations
Impact of using THP
THP improves performance by increasing TLB hit rates and reducing PTE overhead, with no application code changes required. However, when THP allocation fails, the triggered recovery mechanisms can degrade performance. The default configuration does not suit all workloads. For example:
-
With THP defrag set to
always, the system performs synchronous Direct Reclaim or Direct Compaction under memory pressure, causing latency spikes. -
With
khugepagedenabled (1), the daemon acquires memory path locks during consolidation, which can degrade memory-sensitive applications if triggered at the wrong time. -
With THP enabled but both defragmentation features disabled, memory exhausts free pages faster than with 4 KB pages, triggering earlier reclamation and compaction.
Configuration recommendations
THP's performance impact depends on your workload. Adjust the configuration based on your application requirements.
Back up your configuration files or Create a snapshot for a disk before making changes.
-
For a balance between performance and stability, consider the experimental
defer+madvisesetting. This letskswapd,kcompactd, andkhugepagedhandle compaction in the background without blocking applications.sudo bash -c "echo 'defer+madvise' > /sys/kernel/mm/transparent_hugepage/defrag" -
If
khugepagedCPU usage is high (approaching 100%), increase its scan interval. For example, set it to 30 seconds:sudo sh -c 'echo 30000 > /sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs'Alternatively, disable
khugepaged:sudo sh -c 'echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag' -
For workloads where stability outweighs throughput — such as high-volume databases, latency-sensitive services, or frequent short-lived allocations — disable THP. To disable at runtime:
sudo sh -c 'echo "never" > /sys/kernel/mm/transparent_hugepage/enabled'NoteThis change applies only to the current session and reverts on reboot. To disable THP permanently, add a kernel boot parameter:
sudo grubby --args="transparent_hugepage=never" --update-kernel="/boot/vmlinuz-$(uname -r)" sudo reboot
Check THP usage
Check THP usage at the system or process level.
-
System level
THP parameters affect all processes. Check system-wide THP usage:
cat /proc/meminfo | grep AnonHugePagesExample output:
AnonHugePages: 614400 kBNoteA non-zero value indicates that THPs are in use on the system.
-
Process level: Applications use
madvise()withMADV_HUGEPAGEto control THP for their own memory regions without affecting other processes. Check THP usage for a specific process:sudo cat /proc/<PID>/smaps | grep AnonHugePagesNoteReplace
<PID>with the actual process ID.Example output:
AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB AnonHugePages: 0 kB
References
-
THP functionality and risks are documented in Transparent Hugepage Support.
-
Code Huge Page applies the same consolidation principle to map code segments into 2 MB+ regions, reducing TLB misses. Alibaba Cloud Linux also supports Huge Pages for code.
-
Red Hat Enterprise Linux 7 THP tuning: Configuring Transparent Huge Pages.