Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is heap related to process in C?

Tags:

c

linux

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?

like image 241
kadina Avatar asked Oct 03 '14 18:10

kadina


1 Answers

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.

like image 193
Sergey Kalinichenko Avatar answered Oct 11 '22 21:10

Sergey Kalinichenko