CommitLimit 的准确计算公式

CommitLimit 的准确计算公式


✅ 第1条:vm.overcommit_kbytes的优先级高于vm.overcommit_ratio

系统给内核中有两个参数可以设置 vm.overcommit_kbytesvm.overcommit_ratiovm.overcommit_kbytes 的优先级高于 vm.overcommit_ratio

正确。

  • 如果设置了 vm.overcommit_kbytes(单位:KB),系统直接使用这个值来作为 Commit Limit忽略 vm.overcommit_ratio
  • 如果 vm.overcommit_kbytes = 0,则内核使用 vm.overcommit_ratio 进行计算。

✅ 第2条:使用 vm.overcommit_kbytes 时,vm.overcommit_kbytes 配置多少就是多少

使用 vm.overcommit_kbytes 时,vm.overcommit_kbytes 配置多少就是多少。

正确。

  • 这代表可被虚拟内存分配器“承诺”的最大内存量,单位为 KB。
  • 例如:vm.overcommit_kbytes = 52428800,表示 CommitLimit 是 50GB

✅ 第3条:使用 vm.overcommit_ratio 时,CommitLimit = ((物理内存 - 大页内存) * overcommit_ratio / 100) + swap

使用 vm.overcommit_ratio 时,
CommitLimit = ((物理内存 - 大页内存) * overcommit_ratio / 100) + swap
是否正确呢?

基本正确,我们来逐步拆解并解释。


🧮 CommitLimit 的准确计算公式(当未设置 vm.overcommit_kbytes 时):

CommitLimit = ((MemTotal - HugePages_Total * Hugepagesize) * vm.overcommit_ratio / 100) + SwapTotal

其中:

  • MemTotal:从 /proc/meminfo 获取的物理内存总量,单位为 KB。
  • HugePages_Total × Hugepagesize:预留用于大页内存的部分,不能被普通进程使用。
  • vm.overcommit_ratio:百分比。
  • SwapTotal:从 /proc/meminfo 获取 swap 大小。

🔍 举例说明:

假设:

  • 物理内存:MemTotal = 131072000 KB (约 125GB)
  • 大页配置:HugePages_Total = 1024Hugepagesize = 2048 KB(即 2MB 大页 × 1024 = 2GB)
  • vm.overcommit_ratio = 50
  • SwapTotal = 33554432 KB (32GB)

则:

CommitLimit = ((131072000 - 1024*2048) * 50 / 100) + 33554432
            = ((131072000 - 2097152) * 0.5) + 33554432
            = (128974848 * 0.5) + 33554432
            = 64487424 + 33554432
            = 98041856 KB ≈ 93.5 GB

🧾 总结

配置项含义与行为
vm.overcommit_kbytes显式设置 CommitLimit,单位为 KB,优先级最高
vm.overcommit_ratio当未设置 vm.overcommit_kbytes 时,用此百分比计算 CommitLimit
CommitLimit 计算方式((MemTotal - HugePage Reserved) * ratio / 100) + Swap

计算脚本

下面是一个 Shell 脚本,可以动态计算 Linux 系统当前的 CommitLimit,根据是否设置了 vm.overcommit_kbytes 自动选择计算方式:

#!/bin/bash

# 获取当前配置
overcommit_kbytes=$(cat /proc/sys/vm/overcommit_kbytes)
overcommit_ratio=$(cat /proc/sys/vm/overcommit_ratio)

# 获取内存信息(单位:KB)
mem_total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
swap_total=$(grep SwapTotal /proc/meminfo | awk '{print $2}')
hugepages_total=$(grep HugePages_Total /proc/meminfo | awk '{print $2}')
hugepages_size=$(grep Hugepagesize /proc/meminfo | awk '{print $2}')
hugepages_reserved_kb=$((hugepages_total * hugepages_size))

# 根据优先级计算 CommitLimit
if [ "$overcommit_kbytes" -gt 0 ]; then
    commit_limit=$overcommit_kbytes
    method="使用 vm.overcommit_kbytes"
else
    mem_available_for_commit=$((mem_total - hugepages_reserved_kb))
    commit_limit=$(( (mem_available_for_commit * overcommit_ratio / 100) + swap_total ))
    method="使用 vm.overcommit_ratio"
fi

# 输出结果
echo "------------ CommitLimit 计算 ------------"
echo "MemTotal           : ${mem_total} KB"
echo "SwapTotal          : ${swap_total} KB"
echo "HugePages_Total    : ${hugepages_total}"
echo "Hugepagesize       : ${hugepages_size} KB"
echo "HugePage Reserved  : ${hugepages_reserved_kb} KB"
echo "overcommit_kbytes  : ${overcommit_kbytes}"
echo "overcommit_ratio   : ${overcommit_ratio}%"
echo "------------------------------------------"
echo "CommitLimit        : ${commit_limit} KB  ≈ $((commit_limit / 1024 / 1024)) GB"
echo "计算方式           : $method"

✅ 使用方式

  1. 将脚本保存为 calc_commit_limit.sh

  2. 赋予执行权限:

    chmod +x calc_commit_limit.sh
    
  3. 运行:

    ./calc_commit_limit.sh
    

📌 示例输出(样例):

------------ CommitLimit 计算 ------------
MemTotal           : 134217728 KB
SwapTotal          : 33554432 KB
HugePages_Total    : 1024
Hugepagesize       : 2048 KB
HugePage Reserved  : 2097152 KB
overcommit_kbytes  : 0
overcommit_ratio   : 50%
------------------------------------------
CommitLimit        : 100318496 KB  ≈ 95 GB
计算方式           : 使用 vm.overcommit_ratio
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值