0

Hi Anybody knows what is the upper limit for the heap allocation in linux process? Consider below example,

int main() {
    char *p;
    unsigned long int cnt=0;
    while(1) {
        p = (char*)malloc(128*1024*1024); //128MB
        cnt++;
        cout <<cnt<<endl;
    }
    return 0;
}

This program will get killed only after around ~200000 iterations, that means it allocates 128MB*200000=~25TB, my system itself has 512gb of SSD + 6GB of RAM, how this program able to allocate 25TB of memory?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Today's OS use something like lazy allocation. Try doing something with the memory. – Ouroborus Apr 19 '22 at 16:40
  • Thanks a lot, above question clearly answers my question, when I wrote some data into allocated memory its actually exiting after available RAM in my PC has consumed – Sriraj Hebbar Apr 20 '22 at 01:41

1 Answers1

0

Thanks Nate Eldredge for pointing Why doesn't this memory eater really eat memory? It actually consumes the memory only if we wrote some data into it, when I modify above program like this it actually exited after my PC's RAM was fully consumed (which was ~3GB in 4GB RAM system, I guess 1 GB RAM was reserved for kernel)

int main() {
    char *p;
    unsigned long int cnt=0;
    size_t i=0, t = 128*1024*1024;
    while(1) {
        p = (char*)malloc(t);
#if 1
        for (int i=0;i<t;i++)
            *p++ = '0'+(i%65);
#endif
        cnt++;
        cout <<cnt<<endl;
    }
    return 0;
}