hugetlb environment variable (process-level hugepage control), cgroup v2 memory limits, and PSI memory pressure metrics.free -h). Hugepage pre-allocation must be planned based on the physical memory.always may cause occasional latency spikes for latency-sensitive services (such as databases). For container colocation scenarios, madvise is recommended so that applications explicitly request hugepages.hugetlb environment variable requires glibc 2.35 or later. TencentOS Server 4 meets this requirement by default. Earlier glibc versions silently ignore this variable.hugetlb=2) or custom hugepages (hugetlb=<N>), you must pre-allocate nr_hugepages. Otherwise, malloc will fail to obtain hugepages at runtime.hugetlb=<N>) is bytes. To set a 1 GB hugepage, write 1073741824, not 1G.oom_score_adj to -1000 completely prevents the process from being selected by the OOM Killer. However, if the system runs out of memory, the OOM Killer may kill other critical processes instead. It is more recommended to use cgroup memory.max to limit the memory of a single process.memory.high should not be set too low relative to memory.max. Otherwise, it will cause frequent memory reclamation and degrade application performance. It is recommended to set memory.high to 80% to 90% of memory.max.oom_score in the cgroup (usually the one with the largest memory usage), without considering the business importance of the process. Core business processes should be protected through oom_score_adj or cgroup isolation.Interface File | Value Range | Default Value | Read/Write Description |
/sys/kernel/mm/transparent_hugepage/enabled | always/madvise/never | madvise | Read/write: Set the transparent huge page usage policy. always enables it for all processes;madvise is enabled only for explicitly requested regions;never disables it. |
/sys/kernel/mm/transparent_hugepage/defrag | always/defer/defer+madvise/madvise/never | madvise | Read/write: Set the memory defragmentation policy. always continuously defragments;defer defers defragmentation;defer+madvise defers defragmentation for madvise regions;madvise defragments only explicitly requested regions;never does not defragment. |
/sys/kernel/mm/transparent_hugepage/hpage_pmd_size | Read-Only | 2097152(2MB) | Read: View the transparent huge page size in bytes. |
Environment Variable | Value | Meaning |
GLIBC_TUNABLES=glibc.malloc.hugetlb=0 | 0 | Do not use huge pages. |
GLIBC_TUNABLES=glibc.malloc.hugetlb=1 | 1 | Use Transparent Huge Pages (requires THP to be set to madvise). |
GLIBC_TUNABLES=glibc.malloc.hugetlb=2 | 2 | Use system default huge pages (requires preallocating nr_hugepages). |
GLIBC_TUNABLES=glibc.malloc.hugetlb=<N> | N>2 | Use huge pages of a specified size (requires preallocating the corresponding size). Note: The unit is bytes, and the value must equal a hugepage size supported by the system, such as 2097152 or 1073741824. Valid values are determined by the specifications available in the /sys/kernel/mm/hugepages directory. If an unsupported value is written, malloc falls back to regular memory allocation. |
Interface File | Value | Default Value | Description |
/proc/meminfo → Hugepagesize | Read-Only | 2048 kB(2MB) | System default huge page size |
/proc/meminfo → HugePages_Total | Read-Only | 0 | Total number of allocated huge pages |
/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages | Positive integer. | 0 | Read/write: number of reserved 2 MB huge pages |
/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages | Positive integer. | 0 | Read/write: number of reserved 1 GB huge pages |
Interface File | Value Range | Default Value | Read/Write Description |
memory.max | Number of bytes or max | max | Read/Write: memory hard limit. OOM is triggered when the limit is exceeded. |
memory.high | Number of bytes or max | max | Read/Write: memory soft limit. Reclamation is triggered when the limit is exceeded. |
memory.swap.max | Number of bytes or max | max | Read/Write: swap usage limit |
memory.reclaim | Number of Bytes | - | Write-only: writing a specified number of bytes triggers proactive reclamation. |
memory.stat | Read-Only | - | Read: page-level memory usage statistics |
memory.pressure | Read-Only | - | Read: PSI memory pressure metric |
memory.oom.control | 0/1 | 0 | Read/Write: whether to disable the OOM Killer in a cgroup |
Parameter | Position | Value | Description |
/proc/<pid>/oom_score_adj | procfs | -1000 ~ 1000 | Adjusts the probability of a process being selected by the OOM Killer. -1000 means the process is prohibited from being killed. |
/proc/<pid>/oom_score | procfs | Read-Only | Current OOM score (a higher score means a higher probability of being killed) |
vm.panic_on_oom | sysctl | 0/1 | 0=invokes the OOM Killer (default). 1=panics directly. |
vm.oom_kill_allocating_task | sysctl | 0/1 | 0=normal selection process (default). The kernel selects the termination target based on the oom_score, which is typically the process with the largest memory usage, and this target is not necessarily the process that triggered the allocation failure. 1=directly terminates the process that initiated this memory allocation, skipping the oom_score calculation. |
# Confirm the enablement status of transparent hugepagescat /sys/kernel/mm/transparent_hugepage/enabled# Expected output: always [madvise] never (Brackets indicate the current value)# Set to always (system-wide priority for transparent hugepages)echo always > /sys/kernel/mm/transparent_hugepage/enabled# Set to madvise (used only for explicitly requested memory regions, recommended for container scenarios)echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
hugetlb=1 depends on the madvise mode):cat /sys/kernel/mm/transparent_hugepage/enabled# Expected output should include [madvise]
hugetlb environment variable:export GLIBC_TUNABLES=glibc.malloc.hugetlb=1
./your_application
madvise(MADV_HUGEPAGE) to use transparent hugepages for the allocated memory region, reducing TLB misses.cat /proc/meminfo | grep Hugepagesize# Expected output: Hugepagesize: 2048 kB
echo 520 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
hugetlb environment variable:export GLIBC_TUNABLES=glibc.malloc.hugetlb=2
./your_application
ls /sys/kernel/mm/hugepages# Expected output: hugepages-1048576kB hugepages-2048kB
echo 3 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
hugetlb environment variable (in bytes, 1GB=1073741824):export GLIBC_TUNABLES=glibc.malloc.hugetlb=1073741824
./your_application
/var/log/messages or journalctl -k) records detailed OOM information. Key log fields:Log Fields | Description |
invoked oom-killer | Process name that triggered OOM and gfp_mask |
cpuset= / mems_allowed= | cgroup and NUMA memory limits of the process |
CPU: PID: Comm: | PID and command name of the triggering process |
Mem-Info: | System memory usage (in pages; by default, 1 page = 4 KB on x86) |
Node X ... free: ... low: | free/low memory of each NUMA node. free < low is the direct cause of OOM. |
Killed process ... | Process selected by the OOM Killer and its oom_score |
# View recent OOM logsjournalctl -k | grep -i "oom\\|killed process" | tail -20# Adjust the OOM score of a process (to reduce the probability of core business processes being killed)echo -500 > /proc/<pid>/oom_score_adj# Completely prevent a process from being killed by the OOM Killerecho -1000 > /proc/<pid>/oom_score_adj
oom_score_adj is set to -1000, the OOM Killer may kill other processes instead when the system runs out of memory. It is recommended to use cgroup memory.max to limit the memory of a single process.# Create a memory-limited cgroupmkdir /sys/fs/cgroup/myapp# Set a hard limit (1GB)echo 1073741824 > /sys/fs/cgroup/myapp/memory.max# Set a soft limit (512MB, triggers reclamation when exceeded but does not kill the process)echo 536870912 > /sys/fs/cgroup/myapp/memory.high# Add the target process to the cgroupecho $PID > /sys/fs/cgroup/myapp/cgroup.procs# View cgroup memory statisticscat /sys/fs/cgroup/myapp/memory.stat
myapp cgroup exceeds memory.high (512MB), the kernel starts reclaiming memory from this cgroup. When it exceeds memory.max (1GB), the OOM Killer is triggered to kill the process with the highest memory usage in this cgroup, without affecting services in other cgroups.madvise mode so that applications explicitly request hugepages:echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
nr_hugepages based on actual service requirements, and monitor hugepage utilization by running cat /proc/meminfo | grep HugePages. Idle hugepages can be reclaimed:# View hugepage usagegrep -i huge /proc/meminfo# Reduce the number of reserved hugepagesecho 0 > /proc/sys/vm/nr_hugepages
# 1. Check the glibc versionldd --version# 2. Confirm that hugepages are pre-allocatedgrep HugePages_Total /proc/meminfo# 3. If hugepages are insufficient, allocate them first (Note: Pre-allocated hugepages are deducted from the regular memory pool)echo <number_of_pages> > /proc/sys/vm/nr_hugepages
# Method 1: Lower the oom_score of core processes (-1000 means the process is completely protected from being killed. Note that when the system runs out of memory, the OOM Killer will kill other processes instead)echo -1000 > /proc/<PID>/oom_score_adj# Method 2: Use cgroup isolation (Recommended. When the memory.max limit is exceeded, only processes within that cgroup are killed, without affecting other services)echo <limit_bytes> > /sys/fs/cgroup/<cgroup_name>/memory.max
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan