Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should errno be assigned to ENOMEM?

The following program is killed by the kernel when the memory is ran out. I would like to know when the global variable should be assigned to "ENOMEM".

#define MEGABYTE 1024*1024
#define TRUE 1
int main(int argc, char *argv[]){

    void *myblock = NULL;
    int count = 0;

    while(TRUE)
    {
            myblock = (void *) malloc(MEGABYTE);
            if (!myblock) break;
            memset(myblock,1, MEGABYTE);
            printf("Currently allocating %d MB\n",++count);
    }
    exit(0);
}
like image 562
venus.w Avatar asked Jun 10 '12 03:06

venus.w


3 Answers

First, fix your kernel not to overcommit:

echo "2" > /proc/sys/vm/overcommit_memory

Now malloc should behave properly.

like image 187
R.. GitHub STOP HELPING ICE Avatar answered Nov 03 '22 07:11

R.. GitHub STOP HELPING ICE


I think errno will be set to ENOMEM:

Macro defined in stdio.h. Here is the documentation.

#define ENOMEM          12      /* Out of Memory */

After you call malloc in this statement:

myblock = (void *) malloc(MEGABYTE);

And the function returns NULL -because system is out of memory -.

I found this SO question very interesting.

Hope it helps!

like image 36
Cacho Santa Avatar answered Nov 03 '22 07:11

Cacho Santa


It happens when you try to allocate too much memory at once.

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

int main(int argc, char *argv[])
{
  void *p;

  p = malloc(1024L * 1024 * 1024 * 1024);
  if(p == NULL)
  {
    printf("%d\n", errno);
    perror("malloc");
  }
}

In your case the OOM killer is getting to the process first.

like image 23
Ignacio Vazquez-Abrams Avatar answered Nov 03 '22 07:11

Ignacio Vazquez-Abrams