I am reading how malloc() and calloc() are able to allocate memory from heap and I came across a website (http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html) which says that "if you won't free the memory, it will be a memory leak and the memory won't be available for other processes to allocate the memory". But till now I was thinking that heap memory is per process and one process's heap memory is not intermixed with the other process' heap memory. Can any one please let me know whether my understanding is correct?
You are correct, heap memory is per process. However, all processes on the same system allocate memory from the same fixed pool, which is limited to the physical memory of your system plus the swap file on virtual memory systems. That is why if one process holds on to the memory that it does not need, it may starve of memory another process on the same computer.
On systems with virtual memory this does not necessarily mean that other processes will run out of memory, though: it means that getting more memory for these other processes would require swapping other processes out of memory. It may be your leaking process or some other process that must be swapped.
On systems without virtual memory management leaking memory in one process would lead to other processes being unable to allocate memory at all.
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