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.
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.
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.
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.
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.
- Execute the following command to query the watermark ratio and actual watermark of a single existing Memcg.
tip
Replace <memcg_path> with the actual path of the target Memcg.
tip
Replace <cgroup_path> with the actual path of the target Cgroup.
- Execute the following command to configure the watermark ratio for the specified Memcg.
tip
Replace <memcg_path> with the actual path of the target Memcg, and replace 100 with the desired value.
tip
Replace <cgroup_path> with the actual path of the target Cgroup, and replace 100 with the desired value.
This method sets the global default values for subsequently created Memcgs and does not affect existing cgroups. It is suitable for applying uniform default values to new workloads after canary validation has passed.
- Execute the following commands to query the global default watermark ratio and the maximum concurrency of memcg kworkers for subsequently created Memcgs.
warning
The sysctl query result represents only the global default values and does not indicate that asynchronous reclaim has been enabled for existing Memcgs. To verify the actual configuration of an existing cgroup, use the cgroup interface to query.
- Select a configuration method as needed to set the global default values for subsequently created Memcgs.
Configuration by using the proc interface
Configure by using the sysctl file
Execute the following commands on the target Linux instance to configure the memcg_watermark_scale_factor and memcg_mem_reclaim_wq_max_active parameter values via the proc interface.
tip
- This method allows configuration changes to be made and applied while the system is running. However, the configuration will reset after an operating system restart and must be reconfigured.
- The following commands set the default watermark ratio for subsequently created Memcgs to 100 and set the maximum concurrency of memcg kworkers per NUMA node to 1. Replace the values as needed.
Interface description:
- Configure Memcg watermarks via the sysctl configuration file.
warning
This method requires an operating system restart for the configuration to take effect. Once the operating system restarts, the configuration persists and does not need to be reconfigured.
- Execute the following command to open the /etc/sysctl.conf file.
- Press the i key to enter edit mode, and add or update the following parameters.
tip
The following configurations set the default watermark ratio for subsequently created Memcgs to 100 and set the maximum concurrency of memcg kworkers per NUMA node to 1. Replace the values as needed.
- Press the Esc key to exit edit mode, enter :wq, and then press Enter to save and exit the file.
- Execute the following command to make the configuration take effect.
-
This method sets the global default watermark ratio during system boot, applying it to subsequently created Memcgs. It takes effect after the instance is restarted and persists across restarts. It is suitable for scenarios where default configuration should be applied automatically at system boot.
warning
Modifying the GRUB configuration file is a high-risk operation. Incorrect configuration may prevent the system from starting normally. Back up the original configuration file before proceeding, and verify the changes in a test environment before applying them to a production environment.
- Execute the following command to back up the GRUB configuration file.
- Execute the following command to check whether the asynchronous memory reclaim parameters already exist in the GRUB configuration file.
- If the output is empty, execute the following command to append the memcg_watermark_scale_factor parameter.
tip
Replace 100 with the target watermark ratio value.
- If the output is not empty, execute the following command to update the memcg_watermark_scale_factor parameter value.
tip
Replace 100 with the target watermark ratio value.
- Execute the following command to persist the parameter configuration to the GRUB configuration file.
veLinux
veLinux CentOS Compatible