GNU/Linux - /proc/sys/vm/overcommit_memory
/proc/sys/vm/overcommit_memory "是一个 Linux 内核参数,用于控制系统处理内存分配请求的方式。该参数对决定进程请求内存时内核的行为至关重要。让我们来详细了解一下它的含义和影响:
The "/proc/sys/vm/overcommit_memory" is a Linux kernel parameter that controls how the system handles memory allocation requests. This parameter is crucial in determining the kernel's behavior when processes request memory. Let's explore its meaning and implications in detail:
Overview
overcommit_memory 参数决定内核的内存超量分配策略。内存超额分配允许系统为进程分配的虚拟内存超过可用的物理内存(包括交换)。
The overcommit_memory parameter determines the kernel's policy for memory overcommitment. Memory overcommitment allows the system to allocate more virtual memory to processes than the amount of physical memory (including swap) available.
Available Modes
overcommit_memory 有三种可能的值,分别代表不同的超量提交处理模式:
There are three possible values for overcommit_memory, each representing a different overcommit handling mode:
1. 模式 0(默认): 启发式超承诺处理
- 这是默认设置。
- 内核使用启发式方法决定是否允许或拒绝内存分配请求。
- 内核通常允许轻微的超量分配,但拒绝明显的地址空间超量分配。
- 该模式旨在防止疯狂分配,同时减少交换使用。
- 在此模式下,允许根用户分配稍多的内存。
1. Mode 0 (Default): Heuristic overcommit handling
- This is the default setting.
- The kernel uses a heuristic approach to decide whether to allow or refuse memory allocation requests.
- It typically allows slight overcommitment but refuses obvious overcommits of address space.
- This mode aims to prevent wild allocations while reducing swap usage.
- Root users are allowed to allocate slightly more memory in this mode.
2. 模式 1:总是超量分配
- 在这种模式下,无论当前内存使用情况如何,内核总是允许内存分配请求。
- 这种模式适用于某些科学应用程序,尤其是那些使用稀疏阵列(主要由零页组成)的应用程序。
- 这种模式可能会有风险,因为它可能会更频繁地导致内存不足(OOM)的情况。
2. Mode 1: Always overcommit
- In this mode, the kernel always allows memory allocation requests, regardless of the current memory usage.
- It's appropriate for some scientific applications, particularly those using sparse arrays that consist mostly of zero pages.
- This mode can be risky as it may lead to out-of-memory (OOM) situations more frequently.
3. 模式 2:不过度承诺
- 这是最保守的模式。
- 系统提交的总地址空间不允许超过 swap 加上物理 RAM 的可配置百分比(默认为 50%)。
- 当达到限制时,进程将在内存分配时收到错误信息,而不是在访问页面时被杀死。
- 该模式适用于需要保证其内存分配在未来可用的应用程序。
3. Mode 2: Don't overcommit
- This is the most conservative mode.
- The total address space commit for the system is not permitted to exceed swap plus a configurable percentage (default 50%) of physical RAM.
- Processes will receive errors on memory allocation when the limit is reached, rather than being killed while accessing pages.
- This mode is useful for applications that need to guarantee their memory allocations will be available in the future.
Setting and Viewing the Value
您可以使用以下命令查看 overcommit_memory 的当前值:
You can view the current value of overcommit_memory using the following command:
cat /proc/sys/vm/overcommit_memory
要临时更改数值,可以使用
To change the value temporarily, you can use:
echo 1 | sudo tee /proc/sys/vm/overcommit_memory
如需永久更改,请在 /etc/sysctl.conf 中添加以下一行:
For a permanent change, add the following line to /etc/sysctl.conf:
vm.overcommit_memory=x
其中,“x ”为所需值(0、1 或 2)。
Where 'x' is the desired value (0, 1, or 2).
Related Parameters
-
vm.overcommit_ratio: 该参数设置模式 2 时允许超量提交的物理 RAM 百分比。
-
vm.overcommit_kbytes: overcommit_ratio 的替代参数,指定以千字节为单位的超量分配限制。
-
vm.overcommit_ratio: This parameter sets the percentage of physical RAM to allow for overcommitment when in mode 2.
-
vm.overcommit_kbytes: An alternative to overcommit_ratio, specifying the overcommit limit in kilobytes.
在我的Ubuntu里看了下,ratio是50,kbytes是0。
Practical Implications
1. 模式 0(默认): 兼顾大多数系统的性能和稳定性。
2. 模式 1:适用于需要大量虚拟内存但不一定全部使用虚拟内存的应用程序。不过,如果物理内存耗尽,可能会导致系统不稳定。
3. 模式 2:提供最高级别的内存分配保证,但可能会限制某些应用程序的性能。
1. Mode 0 (Default): Balances performance and stability for most systems.
2. Mode 1: Useful for applications requiring large amounts of virtual memory but not necessarily using it all. However, it can lead to system instability if physical memory is exhausted.
3. Mode 2: Provides the highest level of memory allocation guarantee but may limit performance for some applications.
Choosing the Right Mode
超量提交模式的选择取决于具体的使用情况:
- 对于通用系统,默认模式 0 通常比较合适。
- 科学应用或使用稀疏数据结构的应用可能会从模式 1 中受益。
- 需要严格内存分配保证的系统应考虑使用模式 2。
The choice of overcommit mode depends on your specific use case:
- For general-purpose systems, the default mode 0 is usually suitable.
- Scientific applications or those using sparse data structures might benefit from mode 1.
- Systems requiring strict memory allocation guarantees should consider mode 2.
了解并正确配置 overcommit_memory 参数对于优化系统性能和稳定性至关重要,尤其是在有特定内存要求或限制的环境中。
Understanding and properly configuring the overcommit_memory parameter is crucial for optimizing system performance and stability, especially in environments with specific memory requirements or constraints.
资料来源:
[1] vm.overcommit_memory - LinkedIn,
https://www.linkedin.com/pulse/vmovercommitmemory-sanju-debnath
[2] Overcommit Accounting - The Linux Kernel Archives,
https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
[3] Linux Overcommit Modes | Baeldung on Linux,
https://www.baeldung.com/linux/overcommit-modes
[4] Linux memory overcommit details - Stack Overflow,
https://stackoverflow.com/questions/19148296/linux-memory-overcommit-details