How to reboot on OOM

Set the values in sysctl

vi /etc/sysctl.d/oom.conf

vm.panic_on_oom=1
kernel.panic=10

sysctl -p /etc/sysctl.d/oom.conf

Install the compiler

yum install gcc

Create the program

vi oom.c

*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEN_MB 10 * 1024 * 1024
int main(int argc, char **argv){
        int c = 0;
        while (1){
                char *b = malloc(TEN_MB);
                memset(b, TEN_MB, 0);
                printf("Allocated %d MB\n", (++c * 10));
        }
        return 0;
}

Compile the program

gcc -o oom oom.c

Run the program

./oom

Check the memory and verify the logs

watch -n1 "free -m"

tail -f /var/log/messages

Feb 25 18:09:53 node1 kernel: Out of memory: Kill process 6089 (oom) score 629 or sacrifice child
Feb 25 18:09:53 node1 kernel: Killed process 6089, UID 0, (oom) total-vm:1076838980kB, anon-rss:196516kB, file-rss:392kB