0

I have the following piece of code that runs in C on Ubuntu. It counts how many Gigabytes that can be allocated by the OS via malloc().

#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
    int count = 0;
    char* a;

    while (1){
        a = (char*)malloc(1024*1024*1024);
        if (a==NULL) break;
        count++;
    }

    printf("%d\n", count);

    return 0;
}

Surprisingly, when running on my machine, it prints more than 100,000. I feel it is so unreasonable. My RAM is 8GB, my hard disk is about 500 GB, where does this 100,000 come from?

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • You may enjoy [Why is malloc not “using up” the memory on my computer?](http://stackoverflow.com/questions/19991623/why-is-malloc-not-using-up-the-memory-on-my-computer/19991656#19991656) – chux - Reinstate Monica Feb 20 '14 at 23:26

2 Answers2

2

This is memory overcommit:

[...]Under the default memory management strategy, malloc() essentially always succeeds, with the kenrel assuming you're not really going to use all of the memory you just asked for. The malloc()'s will continue to succeed, but not until you actually try to use the memory you allocated will the kernel 'really' allocate it. [...]

If we look at a Linux man page for malloc it says (emphasis mine):

By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. In case it turns out that the system is out of memory, one or more processes will be killed by the OOM killer.

and:

For more information, see the description of /proc/sys/vm/overcommit_memory and /proc/sys/vm/oom_adj in proc(5), and the Linux kernel source file Documentation/vm/overcommit-accounting.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

The operating system allows you to allocate much more memory than it has available. If you actually try to use the memory, it will run out.

If you want to see it run out, try, e.g., doing a memset on the memory after each successful malloc.

Arkku
  • 41,011
  • 10
  • 62
  • 84