'what is the upper limit for process heap memory? [duplicate]

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?



Solution 1:[1]

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sriraj Hebbar