fork error 和 create thread failed 的另一种可能需要关注的点

1. 背景

问题已经伴随了一阵子,板子在free命令看着还有很多剩余空间的时候,却经常出现:

fork: Cannot allocate memory

并且发现有时候,还会存在这样的报错:

CreateThread() failed! Resource temporarily unavailable

2. 问题分析

网上大多数的排查方式指向的是pid数量达到了最大值,类似于下面这个连接。

pid max导致fork: Cannot allocate memory 的分析及解决办法

,但是实际上整个板子里没几个进程。所以即便这么修改了也没用。

也有些说法是这样的:

bash: fork: Cannot allocate memory 问题的处理

实际上指向的都是pid max值的设置。

3. 根因排查

后续发现是系统的overcommit_memory值被人修改过了,也不知道是哪个仁兄改成了2。这个值是管理什么作用的呢?

原文解释如下:

overcommit_memory
Controls overcommit of system memory, possibly allowing processes to allocate (but not use) more memory than is actually available.

0 - Heuristic overcommit handling. Obvious overcommits of address space are refused. Used for a typical system. It ensures a seriously wild allocation fails while allowing overcommit to reduce swap usage. root is allowed to allocate slighly more memory in this mode. This is the default.
1 - Always overcommit. Appropriate for some scientific applications.
2 - Don’t overcommit. The total address space commit for the system is not permitted to exceed swap plus a configurable percentage (default is 50) of physical RAM. Depending on the percentage you use, in most situations this means a process will not be killed while attempting to use already-allocated memory but will receive errors on memory allocation as appropriate.

对应的中文翻译如下:这里需要注意,很多博客复制粘贴实际上是存在问题的,在2这里漏掉了很严重的一个字。

设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存。

4. 问题总结

a. 查资料要广泛。
b. 不要尽信你看到的东西,要对一切有怀疑心态。
c. 系统运维基础也要扎实。

你可能感兴趣的:(linux,overcommit,fork)