Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible for a laptop to even have so much memory? [duplicate]

Tags:

c

memory

malloc

I was messing with malloc calls, and I wondered how much memory could my OS give me. I tried:

int main() {
    char *String = 0;
    String = malloc (100000000000000); // This is 10^14
    if (String)
        printf ("Alloc success !\n");
    else
        printf ("Alloc failed !\n");
    return 0;
}

And... It worked. 10^14 is roughly 18 Terabytes. Is it even possible for a laptop to have so much memory? If that's not possible how can this be explained?

like image 514
Jenkinx Avatar asked Jun 26 '17 19:06

Jenkinx


1 Answers

A 64-bit OS can generate massive amounts of address space. What would limit it?

Backing of address space with physical memory (RAM) is only done when needed.

All the malloc call has to do is return an address. That address need not refer to physical memory until you try to read from it or write to it.

The downside of this behavior is that failing the malloc call is the usually the implementation's only chance to tell you nicely that you can't have the memory you are asking for. After this, about all the system can do is terminate the process when it tries to use more memory than the system can back.

Your implementation almost certainly gives you some way to control this behavior either at system level, for each process, or both.

like image 78
David Schwartz Avatar answered Oct 10 '22 16:10

David Schwartz