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.
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) nothingThis 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With