目录标题
CommitLimit 的准确计算公式
✅ 第1条:vm.overcommit_kbytes的优先级高于vm.overcommit_ratio
系统给内核中有两个参数可以设置
vm.overcommit_kbytes和vm.overcommit_ratio,vm.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 = 131072000KB (约 125GB) - 大页配置:
HugePages_Total = 1024,Hugepagesize = 2048 KB(即 2MB 大页 × 1024 = 2GB) vm.overcommit_ratio = 50SwapTotal = 33554432KB (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"
✅ 使用方式
-
将脚本保存为
calc_commit_limit.sh -
赋予执行权限:
chmod +x calc_commit_limit.sh -
运行:
./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
2173

被折叠的 条评论
为什么被折叠?



