You need to enable JavaScript to run this app.
Elastic Compute Service

Elastic Compute Service

Copy page
Download PDF
Kernel fine-tuning
Configuring background asynchronous memory reclaim for Memcg
Copy page
Download PDF
Configuring background asynchronous memory reclaim for Memcg
Memcg asynchronous memory reclaim is an enhanced feature provided by the veLinux kernel on top of the standard Memory Cgroup. This topic describes the working principle, configuration, and query methods of this feature, helping you reduce the impact of memory reclaim on request latency in latency-sensitive workloads through background asynchronous reclaim.​
warning​
Memcg asynchronous memory reclaim does not increase the cgroup memory limit and cannot replace memory management on the application side. If a workload consistently approaches or exceeds its memory limit (memory.limit_in_bytes), you still need to address it through capacity evaluation, cache limit management, memory leak troubleshooting, and cgroup limit adjustment.​
Background​
Memory Cgroup (Memcg) is the memory subsystem of Linux kernel control groups (cgroups). It isolates the memory behaviors of a group of processes from the rest of the system, and tracks and limits their memory usage. Common use cases include isolating memory-intensive applications, setting memory quotas for containers or VMs, and preventing a single workload from exhausting the memory of an entire node. Learn more​
  • Direct memory reclaim: When Memcg usage reaches its limit, the kernel reclaims memory synchronously on the memory allocation path of the application thread. The thread is blocked during reclaim. Under high memory pressure, this manifests as tail latency jitter, increased memory allocation latency, and occasional service stalls.​
  • Asynchronous memory reclaim: Two memory watermarks, low and high, are configured for the cgroup. When available memory drops to the low watermark, Memcg wakes up a background kworker thread to reclaim memory asynchronously until available memory recovers to the high watermark. The reclaim process does not occupy the memory allocation path of application threads, so threads continue running without being blocked. Memory is freed before usage reaches the limit, reducing the likelihood of direct memory reclaim being triggered (though it cannot be guaranteed to be completely avoided) and minimizing request latency jitter.​
Applicable scenario​
Memcg asynchronous memory reclaim is suitable when a workload uses Memcg for memory isolation and exhibits any of the following symptoms under high memory pressure:​
  • Noticeable jitter in request tail latency (P99, P999).​
  • Increased latency in memory allocation (page fault, malloc).​
  • Occasional service stalls despite available CPU and I/O headroom on the node.​
This feature is especially suitable for workloads that are latency-sensitive, experience significant memory allocation bursts, or require memory isolation between containers or process groups, such as online services, cache services, and real-time data processing.​
Usage notes​
​
Item​
Description​
Kernel version​
veLinux kernel versions that support this feature:​
  • veLinux:​
  • 5.4.250-1-velinux1 and later​
  • 5.15.152.ve.7u1 and later​
  • veLinux CentOS Compatible:​
  • 5.4.250-1.ve1 and later​
  • 5.15.152-7.1.ve2 and later​
tip​
If the memory.watermark_scale_factor or memory.watermark interface, or the corresponding sysctl parameter does not exist in your instance operating system, the current kernel version does not support or has not enabled this feature.​
Cgroup version​
cgroup v1 and cgroup v2 are supported.​
tip​
If your operating system uses cgroup v2, we recommend referring to the Linux community solution first and using the memory.reclaim interface to flexibly control reclaim timing and the amount of memory reclaimed. This helps maximize memory utilization and minimize the impact of memory reclaim.​
Risk warning​
The asynchronous reclaim feature is triggered automatically when memory watermarks reach the configured thresholds. It does not evaluate the appropriateness of the current reclaim timing or the amount of memory reclaimed, and may still have some impact on workload latency.​
​
Procedure​
Configuration recommendations​
We recommend that you first use the cgroup interface to enable asynchronous memory reclaim for the target workload on a canary basis. After verifying the gains and CPU overhead, use sysctl (configuration file) to persist the default values for subsequently created cgroups. Configure via kernel boot parameters only when the workload requires default values to be applied automatically at boot time.​
​
Configuration item​
Recommendation​
Watermark ratio​
Start with a small value on a canary basis, for example, 100. If direct memory reclaim remains frequent, gradually increase the ratio. If CPU overhead increases or reclaim becomes too frequent, decrease the ratio.​
Enablement scope​
We recommend enabling the feature individually for latency-sensitive Memcgs where memory reclaim jitter has already been observed (via the cgroup interface). Enabling it globally via sysctl or kernel boot parameters is not recommended.​
Reclaim concurrency​
Keep the default value of 1. If you need to increase it, verify the CPU overhead and workload latency in a test environment first.​
​
Configuration methods​
​
Configure via the cgroup interface
Configure global sysctl parameters
Configure kernel boot parameters
​
This method applies only to a single existing Memcg. Changes take effect immediately after configuration and are lost when the system restarts or the cgroup is recreated. It is suitable for enabling the feature on a canary basis for individual latency-sensitive workloads, and then expanding the scope after verifying tail latency gains and CPU overhead.​
  1. Execute the following command to query the watermark ratio and actual watermark of a single existing Memcg.​
  • ​
    Cgroup v1
    Cgroup v2
    ​
    tip​
    Replace <memcg_path> with the actual path of the target Memcg.​
    ​
    cat /sys/fs/cgroup/memory/<memcg_path>/memory.watermark_scale_factor​
    cat /sys/fs/cgroup/memory/<memcg_path>/memory.watermark​
    ​
    ​
    ​
  • Interface description:​
  • ​
    Interface​
    Attribute​
    Default value​
    Description​
    memory.watermark_scale_factor​
    Read/write​
    0​
    Configure the watermark ratio for the current Memcg. Valid values range from 0 to 1000.​
    • A value of 0 disables asynchronous reclaim for the cgroup.​
    • ​​​
    • A value of 1000 sets the low watermark to 10% of memory.limit_in_bytes, and the high watermark to twice the low watermark.​
    • ​​​
    memory.watermark​
    Read-only​
    0​
    View the calculated low and high watermark values for the current cgroup to verify that the configuration has taken effect.​
    ​​​
    ​
  1. Execute the following command to configure the watermark ratio for the specified Memcg.​
  • ​
    Cgroup v1
    Cgroup v2
    ​
    tip​
    Replace <memcg_path> with the actual path of the target Memcg, and replace 100 with the desired value.​
    ​
    echo 100 > /sys/fs/cgroup/memory/<memcg_path>/memory.watermark_scale_factor​
    ​
    ​
    ​
​
Last updated: 2026.09.08 14:36:36