OOM Killer

 

 

找出最可能被杀掉进程

#!/bin/bash

printf "\e[5m%9s %9s %s\e[0m\n\n" "oom_score" "PID" "cmdline"
for proc in $(find /proc/ -maxdepth 1 -regex '/proc/[0-9]+');do
        printf "%9d %9d %s\n" \
        "$(cat $proc/oom_score)" \
        "$(basename $proc)" \
        "$(cat $proc/cmdline | tr '\0' ' ' | head --bytes 50)"
done 2>/dev/null | sort -nr | head -10

  

simulation:

#include <stdlib.h>
#include <stdio.h>
#define BYTES (16*1024*1024)
#define true 1

int main(void)
{
        printf("oom !\n");
        while(true){
                char *p=malloc(BYTES);
                if (p == NULL){
                        return -1;
                }
                printf("malloc memory pointer: %p, value: %18d, hex: %x\n",p,p,p);
        }
        return 0;
}

 

  

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

#define BYTES (8*1024*1024)
#define N (10240)

int main(void)
{
        printf(" malloc oom\n");
        int i;
        for (i=0;i<N;++i)
        {
                char *p=malloc(BYTES);
                if (p==NULL)
                {
                        return -1;
                }
        }
        printf(" endless lopp\n");
        while(1);
        return 0;
}

 

posted @ 2021-03-26 17:51  ascertain  阅读(58)  评论(0编辑  收藏  举报