Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic memory allocation. What am I missing?

Please take a look at this code:

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

int main()
{
    char* foo = (char*)malloc(500000000);

    // when I uncomment stuff that's below then operating system
    // admits that this program uses 500MB of memory. If I keep
    // this commented, it claims that the program uses almost no
    // memory at all. Why is it so?

    /*
    for (int i=0; i<500000000; i++)
    {
        foo[i] = (char)i;
    }
    */

    int bar; scanf("%d", &bar); // wait so I can see what's goin on

    free(foo);

    return 0;
}

My intuition is simple. When I allocate 500MB with malloc call then OS should say that the process is using over 500MB of memory. But apparently, it doesn't work that way. What am I missing? What trick is OS using, what should I read about?

Thank you in advance for any clues.

like image 516
chomzee Avatar asked Dec 04 '25 17:12

chomzee


1 Answers

What am I missing? What trick is OS using, what should I read about

It's a form of lazy allocation. In a nutshell:

  • malloc asks the OS for a lot of memory and the OS goes: "sure, here you go" and does (almost) nothing
  • the OS secretly hopes you never touch the "allocated" pages
  • when you do touch an allocated page, the OS catches the inevitable page fault, sighs and allocates the page

This happens per-page. So you'll get the same usage if in your for you increment i by the page size on your system (likely 4096 or something like that). As a simple trick, try playing with the number of elements the for touches. As a bonus, try to predict the memory usage by dividing the size by the size of the page

like image 78
cnicutar Avatar answered Dec 06 '25 08:12

cnicutar