问题描述:
SUSE Linux中(SLES 11),激活overcommit memory后,系统会启用oom-killer随机杀死系统进程,在/proc下有一非常大的kcore文件。
问题分析:
请参考文档:
Situation
Resolution
/usr/src/linux/Documentation/vm/overcommit-account
0 - Heuristic overcommit handling.
1 - Always overcommit.
2 - Don't overcommit.
Total Commit Memory = (swap size + (RAM size * overcommit_ratio))
Note - The RedHat documentation, , is a good source of information on overcommit memory. (Other topics in that documentation have evolved since 2004.) However, there is an error in the "overcommit_ratio" section of this document. In this section, the calculation used to determine the allocatable memory is correct. However, in the text accompanying the calculation, the total amount of allocatable memory is incorrectly calculated as 2.5GB (on a server with 1GB of RAM and 1GB of swap space). 1.5GB is the correct value.
/proc/sys/vm/overcommit_memory
/proc/sys/vm/overcommit_ratio
CommitLimit - Overcommit limit
Committed_AS - Current memory amount committed
这是讲overcommiting memory 的几种类型,可以激活也可以禁用,overcommiting memory 的原理就是让系统能够使用超出其实际内存容量的内存,以让更多的程序能够运行,因为不是所有程序都会同时消耗内存的,这个跟Thin Provision有点类似,但是在内存少的情况下,这个多出来的内存如果太多,会激活oom-killer。
以下是overcommit memory的说明:
overcommit_memory is a value which sets the general kernel policy toward granting memory allocations. If the value is 0, then the kernel checks to determine if there is enough memory free to grant a memory request to a malloc call from an application. If there is enough memory, then the request is granted. Otherwise, it is denied and an error code is returned to the application. If the value is set to 1, then the kernel grants allocations above the amount of physical RAM and swap in the system as defined by the overcommit_ratio
value. Enabling this feature can be somewhat helpful in environments which allocate large amounts of memory expecting worst case scenarios but do not use it all. If the setting in this file is 2, the kernel allows all memory allocations, regardless of the current memory allocation state.
解决办法:
用ps查看各进程的内存,大约就占用了4G, 绝大部分内存都是被Page Cache所占用。Linux内核的策略是最大程度的利用内存cache 文件系统的数据,提高IO速度,虽然在机制上是有进程需要更大的内存时,会自动释放Page Cache,但不排除释放不及时或者释放的内存由于存在碎片不满足进程的内存需求。
所以我们需要一个方法,能够限定PageCache的上限。
Linux 提供了这样一个参数min_free_kbytes,用来确定系统开始回收内存的阀值,控制系统的空闲内存。值越高,内核越早开始回收内存,空闲内存越高。
[root@zyite-app01 root]# cat /proc/sys/vm/min_free_kbytes 163840 echo 963840 > /proc/sys/vm/min_free_kbytes |
其他可选的临时解决方法:
关闭oom-killer
echo "0" > /proc/sys/vm/oom-kill_allocating_task
vi /etc/sysctl.conf
vm.oom-kill _ allocating_task = 02. 清空cache (可选)
echo 1 > /proc/sys/vm/drop_caches另:
可以关闭特定程序的OOM killer,以下是示范脚本:
for pid in $(pidof sshd) ; do
echo "disabling oom on pid $pid"
echo -17 | sudo tee /proc/$pid/oom_adj > /dev/null
done
参考文档: