简体   繁体   English

Linux OOM杀手不起作用

[英]Linux OOM killer does not work

I would like to test if the kernel OOM killer work fine on my embedded Linux or not. 我想测试内核OOM杀手是否在我的嵌入式Linux上运行良好。 I used an application test to fill all memory and see if OOM will kill my application if the system run in out of memory condition. 我使用应用程序测试来填充所有内存,看看如果系统在内存不足的情况下运行,OOM是否会终止我的应用程序。

The test program I used: 我用过的测试程序:

#include <stdio.h>
#include <stdlib.h>

#define MEGABYTE 1024*1024

int main(int argc, char *argv[])
{
        void *myblock = NULL;
        int count = 0;

        while(1)
        {
                myblock = (void *) malloc(MEGABYTE);
                if (!myblock) break;
                memset(myblock,1, MEGABYTE);
                printf("Currently allocating %d MB\n",++count);

        }
        exit(0);

}

Results: 结果:

I always get : 我总是得到:

MyApplication triggered out of memory codition (oom killer not called): gfp_mask=0x1200d2, order=0, oomkilladj=0

I try to change /etc/sysctl by adding : 我尝试通过添加以下内容来更改/ etc / sysctl:

vm.oom_kill_allocating_task=1
vm.panic_on_oom=0
vm.overcommit_memory=0
how can I make OOM works fine on my system 

Kernel version :2.6.30 #7 SMP PREEMPT 内核版本:2.6.30#7 SMP PREEMPT

The Linux “OOM killer” is a solution to the overcommit problem . Linux“OOM杀手”是解决过度使用问题的解决方案。

If you just “fill all memory”, then overcommit will not show up. 如果你只是“填满所有记忆”,那么过度使用就不会出现。 The malloc call will eventually return a null pointer, the convention to indicate that the memory request cannot be fulfilled. malloc调用最终将返回一个空指针,该约定指示无法满足内存请求。

In order to cause an overcommit-related problem, you must allocate too much memory without writing to it , and then decide to write to all of it, so that the system finds itself forced to honor promises it made without having the capacity to fulfill them. 为了引起与过度使用相关的问题,你必须在没有写入内容的情况下分配太多内存,然后决定写入所有内存,以便系统发现自己被迫遵守它所做的承诺而没有能力来实现它们。

EDIT after source code was provided: 提供源代码后编辑:

To be completely precise, in order to trigger a problem with overcommit and force the Linux OOM killer to take action, you should have several processes that in a first phase all reserve memory with malloc() (but do not write to it yet). 为了完全准确,为了触发过度使用的问题并强制Linux OOM杀手采取行动,你应该有几个进程在第一阶段都使用malloc()保留内存(但是还没有写入它)。 Then have all of them write to the memory they have reserved at the same time. 然后让他们全部写入他们同时保留的内存。 This will force Linux to honor the memory promises outside of any memory allocation, and it will have no choice but to kill a process that wasn't allocating (since none of them will be allocating at that moment). 这将迫使Linux在任何内存分配之外兑现内存承诺,并且它将别无选择,只能杀死未分配的进程(因为那时它们都不会分配)。

Also, if you still want to see how or when OOM-killer works. 此外,如果您仍想查看OOM杀手如何或何时起作用。 I would suggest you to add fork() before while loop. 我建议你在while循环之前添加fork() That will create many processes, and eventually one of them OOM-killer will kill. 这将创建许多进程,并最终其中一个OOM杀手将杀死。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM