Ole*_*nge 12 linux swap virtual-memory ram
即使有可用的交换,我的服务器也耗尽了内存。
为什么?
我可以这样重现它:
eat_20GB_RAM() {
perl -e '$a="c"x10000000000;print "OK\n";sleep 10000';
}
export -f eat_20GB_RAM
parallel -j0 eat_20GB_RAM ::: {1..25} &
Run Code Online (Sandbox Code Playgroud)
当稳定(即所有进程都进入睡眠状态)时,我再运行一些:
parallel --delay 5 -j0 eat_20GB_RAM ::: {1..25} &
Run Code Online (Sandbox Code Playgroud)
当稳定(即所有进程都进入睡眠状态)时,使用大约 800 GB RAM/swap:
$ free -m
total used free shared buff/cache available
Mem: 515966 440676 74514 1 775 73392
Swap: 1256720 341124 915596
Run Code Online (Sandbox Code Playgroud)
当我再运行一些时:
parallel --delay 15 -j0 eat_20GB_RAM ::: {1..50} &
Run Code Online (Sandbox Code Playgroud)
我开始得到:
Out of memory!
Run Code Online (Sandbox Code Playgroud)
即使有明显的交换可用。
$ free
total used free shared buff/cache available
Mem: 528349276 518336524 7675784 14128 2336968 7316984
Swap: 1286882284 1017746244 269136040
Run Code Online (Sandbox Code Playgroud)
为什么?
$ cat /proc/meminfo
MemTotal: 528349276 kB
MemFree: 7647352 kB
MemAvailable: 7281164 kB
Buffers: 70616 kB
Cached: 1503044 kB
SwapCached: 10404 kB
Active: 476833404 kB
Inactive: 20837620 kB
Active(anon): 476445828 kB
Inactive(anon): 19673864 kB
Active(file): 387576 kB
Inactive(file): 1163756 kB
Unevictable: 18776 kB
Mlocked: 18776 kB
SwapTotal: 1286882284 kB
SwapFree: 269134804 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 496106244 kB
Mapped: 190524 kB
Shmem: 14128 kB
KReclaimable: 753204 kB
Slab: 15772584 kB
SReclaimable: 753204 kB
SUnreclaim: 15019380 kB
KernelStack: 46640 kB
PageTables: 3081488 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1551056920 kB
Committed_AS: 1549560424 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 1682132 kB
VmallocChunk: 0 kB
Percpu: 202752 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 12251620 kB
DirectMap2M: 522496000 kB
DirectMap1G: 3145728 kB
Run Code Online (Sandbox Code Playgroud)
Ole*_*nge 20
在/proc/meminfo你发现:
CommitLimit: 1551056920 kB
Committed_AS: 1549560424 kB
Run Code Online (Sandbox Code Playgroud)
所以你处于提交限制。
如果您已通过以下方式禁用过度使用内存(以避免 OOM 杀手):
echo 2 > /proc/sys/vm/overcommit_memory
Run Code Online (Sandbox Code Playgroud)
然后提交限制计算如下:
2 - Don't overcommit. The total address space commit
for the system is not permitted to exceed swap + a
configurable amount (default is 50%) of physical RAM.
Depending on the amount you use, in most situations
this means a process will not be killed while accessing
pages but will receive errors on memory allocation as
appropriate.
Run Code Online (Sandbox Code Playgroud)
(来自:https : //www.kernel.org/doc/Documentation/vm/overcommit-accounting)
您可以通过以下方式使用完整内存:
echo 100 > /proc/sys/vm/overcommit_ratio
Run Code Online (Sandbox Code Playgroud)
然后,当物理 RAM 和交换空间全部保留时,您将出现内存不足的情况。
这个名字overcommit_ratio在这种情况下有点误导:你没有过度承诺任何东西。
即使使用此设置,您也可能会在交换用完之前看到内存不足。malloc.c:
CommitLimit: 1551056920 kB
Committed_AS: 1549560424 kB
Run Code Online (Sandbox Code Playgroud)
编译为:
gcc -o malloc malloc.c
Run Code Online (Sandbox Code Playgroud)
运行方式(保留 1 GB 10 秒):
./malloc 1073741824 10
Run Code Online (Sandbox Code Playgroud)
如果你运行它,你可能会看到 OOM,即使没有交换:
# Plenty of ram+swap free before we start
$ free -m
total used free shared buff/cache available
Mem: 515966 2824 512361 16 780 511234
Swap: 1256720 0 1256720
# Reserve 1.8 TB
$ ./malloc 1800000000000 100 &
Bytes: 1800000000000 Sleep: 100
# It looks as if there is plenty of ram+swap free
$ free -m
total used free shared buff/cache available
Mem: 515966 2824 512361 16 780 511234
Swap: 1256720 0 1256720
# But there isn't: It is all reserved (just not used yet)
$ cat /proc/meminfo |grep omm
CommitLimit: 1815231560 kB
Committed_AS: 1761680484 kB
# Thus this fails (as you would expect)
$ ./malloc 180000000000 100
Bytes: 180000000000 Sleep: 100
Out of memory
Run Code Online (Sandbox Code Playgroud)
因此,虽然free在实践中经常会做正确的事情,但查看 CommitLimit 和 Committed_AS 似乎更防弹。