Malloc behaves differently on different machines

I see a completely different behavior when starting part of a program that tries to exceed RSS on different machines. The code looks something like this:

...
  char** s = (char**)malloc(10000*sizeof(char*));

  for (i = 0; i < 10000; i++){
    s[i] = (char*)malloc(1000*1000*sizeof(char));
    if (s[i] == NULL) {
      printf("cannot allocate memory for s[%d]",i);
      exit(1);
    }
  }

  int j = 0;
  while(1){
    for (i = 0; i < 10000; i++){
      for (j = 0; j < 1000*1000; j++) {
        s[i][j] = 1;
      }
      if ((i % 100) == 0) printf("i = %d\n", i);
    }
  }
  for (i = 0; i < 10000; i++)
    free(s[i]);
  free(s);
...

The above code is trying to allocate about 10 GB of memory using malloc. The first two machines I tried this code when running on linux kernel 2.6, and the last one ran linux kernel 2.4. Here is the behavior that I see on these machines:

Machine1: memory is allocated using memory overcommit, but when assigning values ​​to memory cells in a while loop, it allocates as much as RSS allows. Thus, OOM Killer kills the process when i = 3800 is printed, which is about 4 GB of memory that this machine has.

Machine2: overcommit while , . , = 3800 , .

machine3: 2 . . , over commit ​​2.4 malloc! , = 2138

- , machine2. - , (kernel?) , malloc , RSS?

+5
1

100 32- , , -, . , 1 , 4 , 2 , 2 64- .

+5

All Articles