Tuesday, May 20, 2014

Using C program to allocate memory using malloc on Linux to test OOM killer behavior

Sometimes we want to manually allocate memory on Linux system to test OOM killer's behavior.

Theory:

In OS with below kernel parameters:
vm.overcommit_memory = 2
vm.overcommit_ratio = 50
which means, OS will at most allocate virtual memory to:  SWAP size +50% RAM size.

For example, if OS has 48G RAM with 48G SWAP as shown below:
[root]# free
             total       used       free     shared    buffers     cached
Mem:      49316552     841556   48474996          0     150432     466384
-/+ buffers/cache:     224740   49091812
Swap:     50307508          0   50307508
Which means, OS will at most allocate 48G+50%*48G = 72G virtual memory.

Experiment:

Let's do a simple test to prove that.
1.  Prepare "mem.c" C program which can allocate memory using function malloc:
#include <stdio.h>
#include <stdlib.h>
int main(){
 int *ptr;
 unsigned long i,n;
 printf("Enter number of int(4 byte) you want to allocate:");
 scanf("%lu",&n);

 printf("Allocating %lu bytes......\n",n*sizeof(int));
 ptr=(int*)malloc(n*sizeof(int));
 if (ptr==NULL){
   printf("ERROR!Memory not allocated!");
   exit(0);
 }
 printf("Filling int into memory.....\n");
 for (i = 0; i < n; i++){
    ptr[i] = 1;
 }
 printf("Sleep 10 seconds......\n");
 sleep(10);
 printf("Free memory.\n");
 free(ptr);
 return 0;
}
2.  Compile it:
gcc mem.c -o mem 
3.  If allocating 40G memory,  all of them are from RAM.
[root]# ./mem
Enter number of int(4 byte) you want to allocate:10000000000
Allocating 40000000000 bytes......
Filling int into memory.....
Sleep 10 seconds......
Free memory.
Before :
[root]# free
             total       used       free     shared    buffers     cached
Mem:      49316552     841556   48474996          0     150432     466384
-/+ buffers/cache:     224740   49091812
Swap:     50307508          0   50307508
After :
[root]# free
             total       used       free     shared    buffers     cached
Mem:      49316552   39978952    9337600          0     150432     466408
-/+ buffers/cache:   39362112    9954440
Swap:     50307508          0   50307508
4.  If allocating 72G memory, 50% SWAP and all RAM are used up.
Note, previously we mentioned that the total virtual memory size is SWAP size +50% RAM size, that is only for calculating the size. In fact, RAM is firstly used before starting to use SWAP.
[root]# ./mem
Enter number of int(4 byte) you want to allocate:18000000000
Allocating 72000000000 bytes......
Filling int into memory.....
Sleep 10 seconds......
Free memory.
After:
[root]# free
             total       used       free     shared    buffers     cached
Mem:      49316552   49179688     136864          0        700      21856
-/+ buffers/cache:   49157132     159420
Swap:     50307508   21593608   28713900
5.  If allocating 80G memory,OOM killer shows up.
[root]# ./mem
Enter number of int(4 byte) you want to allocate:20000000000
Allocating 80000000000 bytes......
ERROR!Memory not allocated!

So one suggestion after adding more physical memory, is to add more SWAP also.

2 comments:

Popular Posts