oom linux 导致ssh,Linux OOM

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

某个机器看到一条日志:Out of Memory: Kill process xxx (xxx) score 707 or sacrifice child

并且syslog, ssh等进程都被kill掉了.

简单了解了下OOM(Out of Memory)相关的问题

在Linux下,如果系统不足以分配程序申请的内存, OOM Killer就会被调用, 它会选择某个进程然后杀掉.

man 5 proc可以看到:

在Linux 2.6.11后, /proc/[pid]/oom_adj 可以控制oom的优先级等, 从-16到15, 数值越小, 被kill的可能性越低. 特殊值-17表示对当前进程禁用oom killer.

在Linux 2.6.36后, /proc/[pid]/oom_adj 被弃用了, 改为 /proc/[pid]/oom_score_adj, 值从-1000到1000, 越小被kill概率越小, -1000表示不被kill.

一般的进程 /proc/[pid]/oom_score_adj 值是0, 像ssh是-1000

关于vm.overcommit_memory, man 5 proc也可以看到:

/proc/sys/vm/overcommit_memory

This file contains the kernel virtual memory accounting mode. Values are:

0: heuristic overcommit (this is the default)

1: always overcommit, never check

2: always check, never overcommit

In mode 0, calls of mmap(2) with MAP_NORESERVE are not checked, and the default check is very weak, leading to the risk of getting a process "OOM-

killed". Under Linux 2.4, any nonzero value implies mode 1.

In mode 2 (available since Linux 2.6), the total virtual address space that can be allocated (CommitLimit in /proc/meminfo) is calculated as

CommitLimit = (total_RAM - total_huge_TLB) *

overcommit_ratio / 100 + total_swap

where:

* total_RAM is the total amount of RAM on the system;

* total_huge_TLB is the amount of memory set aside for huge pages;

* overcommit_ratio is the value in /proc/sys/vm/overcommit_ratio; and

* total_swap is the amount of swap space.

默认是0; 如果设置为2, 则不会出现nevercommit. 这个overcommit, 涉及到一个如上计算公式.

写了一个小程序用来耗尽内存:

$ more memtest.c

#include

#include

int main() {

int *p;

while(1) {

int inc=10*sizeof(char);

p=(int*) calloc(1,inc);

*p = 123456;

if(!p) break;

}

}

默认情况下一会程序就被killed掉了, 如果设置另外一个进程的oom_score_adj为1000, 则这个进程先被杀掉.

这篇文章的9.6节讲得挺好的, 还有这个回答

当然, 还有问题,为何ssh等也都被杀掉了? 很奇葩

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值