Reserve memory for the page cache to resolve page cache thrashing caused by a large number of anonymous page allocations

更新时间:
复制 MD 格式

This topic explains how to reserve memory for the page cache to prevent page cache thrashing caused by large allocations of anonymous pages. This technique helps avoid system stability issues, such as Out of Memory (OOM) kills, that result from continuous page cache reclamation.

Alibaba Cloud Linux 3 introduces the vm.min_cache_kbytes parameter. This feature has undergone two major updates across different kernel versions:

  1. Feature introduction: Kernel version 5.10.112-11 introduced the vm.min_cache_kbytes parameter and its manual configuration.

  2. Adaptive default value: Kernel version 5.10.134-18 and later enhances this feature by automatically setting a suitable default reserved value based on the total memory of the ECS instance.

Limits

  • Operating system: Alibaba Cloud Linux 3

  • Basic feature support: kernel version ≥ 5.10.112-11 (manual configuration required)

  • Automatic default value support: kernel version ≥ 5.10.134-18

Default configuration

If you use kernel version 5.10.134-18 or later, the system automatically sets the value of vm.min_cache_kbytes based on the instance's total physical memory. The default mappings are as follows:

Total memory

Default value (min_cache_kbytes)

(0, 4 GB]

150 MB

(4 GB, 8 GB]

300 MB

(8 GB, 16 GB]

400 MB

(16 GB, 128 GB]

500 MB

> 128 GB

1024 MB (1 GB)

Important
  • These default values are based on data from common use cases. In most scenarios, the default configuration is sufficient for stable system operation.

  • If your kernel version is between 5.10.112-11 and 5.10.134-18, this parameter exists but the default values are not automatically applied. Its initial value is typically 0, so you must configure it manually.

Tuning recommendations

Regardless of the version you are using, if you encounter the following situations while your services are running:

  • The system frequently triggers OOM kills under the current configuration.

  • File I/O performance jitters due to excessive page cache reclamation.

We recommend that you manually adjust the vm.min_cache_kbytes parameter based on your actual memory usage.

Warning
  • Risk of setting the value too high: Reserving too much memory for the page cache reduces the available memory for user-space applications (anonymous page space). This can cause premature OOM events and terminate normal application processes.

  • Risk of setting the value too low: A value that is too small (or 0) may not effectively mitigate page cache thrashing, which can lead to system instability under high load.

  • Recommendation: Always adjust this parameter cautiously based on your specific workload, memory monitoring data, and current kernel version.

Procedure

Replace <KB> with the amount of memory you want to reserve, specified in kilobytes.

  • For example, to reserve 1 GB for the page cache, set <KB> to 1048576.

  • <KB> value of 0 disables this reservation.

Method 1: Use the /proc interface

sudo sh -c 'echo <KB> > /proc/sys/vm/min_cache_kbytes'

Method 2: Use sysctl

sudo sysctl -w vm.min_cache_kbytes=<KB>