How does this program manage to allocate so much memory?

Viewed 90

I was curious to understand how much memory can I allocate on my laptop, therefore I ran the minimalist program:

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

int main() {
    int Gb = 0;
    while ( malloc(1<<30) ){
            ++Gb;
            printf("Now allocated %d Gb in total\n", Gb);
    }
}

It should allocate 1Gb of memory at every step, until the malloc fails (NULL), ending the loop. On the other hand, the output that I obtain ends with:

Now allocated 131063 Gb in total

and it cannot definitely be true (I have 8Gb of RAM and no swap partition). Can I ask, what am I misunderstanding?

EDIT: I forgot to mention, I am working with Linux.

1 Answers

The wonders of virtual memory. Until you actually initialize it somehow, memory doesn't need to take up any RAM. You'll get a far different result from

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

int main(void) {
    unsigned num_blocks = 0;
    for (char *p; ( p = malloc(1<<30) ); ) {
       memset(p, 0, 1<<30);  // <-----
       ++num_blocks;
       printf("Now allocated %u GiB in total\n", num_blocks);
    }
}

Allocating more more virtual memory than the machine has physical memory is known as memory overcommitment.

There's some interesting information found in the article Virtual memory settings in Linux - The Problem with Overcommit.

Related