Reputation: 31
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.
Upvotes: 2
Views: 634
Reputation: 182794
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
Upvotes: 7
Reputation: 2337
It has nothing to do with OS. The compiler optimizes the resulting code such that if the allocated memory is not used then it removes the statement that allocates the memeory in the resulting binary. You can check this by setting your compilers optimization level to zero. Then you will get 500MB in either case because the memory will be allocated every time.
Upvotes: -1
Reputation: 1234
Your OS is likely not allocating (or showing that it has allocated) the memory until you use it.
In any case, checking the return value of malloc()
is a pretty good idea if you're gonna allocate such large chunks. malloc()
can fail, you know.
Upvotes: 1
Reputation: 4231
The operating system memory pages are only really allocated to your process when you access them (by writing, in your case). The exact behavior depends on you compiler and OS - on a different system you might find that the memory is used up immediately.
Upvotes: 1
Reputation: 598448
All memory usage in a process is virtualized by the OS. You might be "allocating" a memory block in your code, but the OS might not actually be committing it to physical memory until it is actually used by the code.
Upvotes: 2