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.
资料来源:
[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
### 如何在 Shell 脦本中调用 add_swap 函数 在 Shell 脚本中,函数的定义和调用是非常常见的操作。以下是关于如何定义并调用 `add_swap` 函数的具体方法。 #### 定义函数 在 Shell 脚本中,函数可以通过两种方式定义: 1. **使用关键字 `function` 的方式** ```bash function add_swap() { echo "This is the add_swap function" # 可以在此处添加具体逻辑 } ``` 2. **直接命名的方式** ```bash add_swap() { echo "This is the add_swap function" # 添加具体的实现细节 } ``` 上述两种方式均有效[^3],可以根据个人习惯选择其中一种方式进行定义。 #### 调用函数 一旦函数被定义,就可以通过其名称直接调用来执行相应的代码块。例如: ```bash add_swap ``` 如果该函数需要传入参数,则可以在调用时附加这些参数。例如: ```bash add_swap param1 param2 ``` 在函数内部,可以使用 `$1`, `$2` 等变量来访问传递给它的参数[^1]。 #### 返回值处理 函数可以通过 `return` 语句返回一个整数值作为退出状态码。如果没有显式指定返回值,默认会返回最后一行命令的退出状态码。例如: ```bash add_swap() { echo "Executing add_swap..." return 0 } echo $? # 输出上一命令的退出状态码 ``` 此外,也可以利用标准输出将数据返回给调用者。例如: ```bash result=$(add_swap) echo "$result" ``` #### 示例完整脚本 以下是一个完整的示例脚本,展示了如何定义和调用 `add_swap` 函数: ```bash #!/bin/bash # 定义函数 add_swap() { local size=$1 if [[ -z "$size" ]]; then echo "Error: No swap size specified." return 1 fi fallocate -l "${size}M" /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo "Swap of size ${size}MB added successfully." } # 调用函数 if [[ $# -eq 0 ]]; then echo "Usage: $0 <swap_size_in_MB>" exit 1 fi add_swap "$1" exit_code=$? if [[ $exit_code -ne 0 ]]; then echo "Function execution failed with code $exit_code." exit $exit_code fi ``` 此脚本接受一个参数(交换分区大小),并通过 `add_swap` 函数创建一个新的交换文件。 --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值