Updated on 2025-07-21 GMT+08:00

Related Settings

Configuring Whether to Enable THP

You can configure whether to enable Transparent Huge Page (THP).

The configuration file is:

/sys/kernel/mm/transparent_hugepage/enabled

Options:

  • always: THP is enabled for all memory allocations whenever possible.
  • madvise: THP is enabled only when a process explicitly requests huge pages through the madvise() system call.
  • never: THP is disabled.

Defragmentation

THP requires contiguous physical memory areas. However, memory fragments are generated due to frequent memory allocation and release after a system runs for a long time. Defragmentation is a process where the kernel merges scattered physical pages by means such as page migration to form a large, contiguous memory area for hugepage allocation. Defragmentation may cause system latency to increase, especially when the defragmentation is performed in the foreground.

The configuration file is:

/sys/kernel/mm/transparent_hugepage/defrag

Options:

  • always: A thread always waits for the kernel to defragment and allocate hugepages. This may slow the response.
  • defer: A thread does not wait for defragmentation. Defragmentation is performed asynchronously in the background.
  • madvise: Defragmentation is only performed for the memory area that explicitly calls madvise().
  • defer+madvise: A thread does not wait for defragmentation. Defragmentation is performed in the background. Defragmentation is only performed for the memory area that explicitly requests to use hugepages.
  • never: Defragmentation is not used at all. If the kernel fails to allocate a hugepage, it will fall back to using small pages.

A Background Thread for Merging Pages

khugepaged is a kernel thread. It periodically scans anonymous memory and automatically merges appropriate 4 KB pages into 2 MB hugepages.

The configuration file is stored in:

/sys/kernel/mm/transparent_hugepage/khugepaged/

Key configuration files:

  • defrag: whether to enable or disable khugepaged.
    • 1: Enable khugepaged to proactively defragment memory.
    • 0: Disable khugepaged from proactively defragmenting memory.
  • alloc_sleep_millisecs: retry interval (unit: ms). This parameter specifies the interval between two khugepaged attempts to allocate huge pages. The default value is 60000.
  • pages_to_scan: number of pages to be scanned. This parameter specifies the number of pages scanned by khugepaged each time it is woken up. The default value is 4096.
  • scan_sleep_millisecs: scanning interval (unit: ms). This parameter is used to adjust the scanning frequency. The default value is 10000.

If services are sensitive to latency, you can increase the scanning interval or disable khugepaged.