Is heap related to process in C?

I read how malloc () and calloc () can allocate memory from the heap, and I stumbled upon a website ( http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html ) that says "if you don't free memory, it will be a memory leak, and memory will not be available for other processes to allocate memory. " But so far, I thought that heap memory is on each process, and one heap of process memory is not mixed with other process heap memory. Can someone please tell me if I understand correctly?

+5
source share
4 answers

You are right, the memory of the heap is for the process. However, all processes in the same system allocate memory from the same fixed pool, which is limited by the physical memory of your system plus the page file in virtual memory systems. That is why, if one process is held in memory in which it is not needed, it may starve out of the memory of another process on the same computer.

On systems with virtual memory, this does not necessarily mean that there is not enough memory in other processes: this means that to get more memory for these other processes, switching other processes from memory will be required. This could be your ongoing process or some other process that needs to be replaced.

On systems without virtual memory management, a memory leak in one process will cause other processes to not allocate memory at all.

+6
source

Well, this memory must come from somewhere - physical RAM. Thus, for the life of your process, if you do not free this memory, physical RAM is allocated for it.

So, although the heap belongs to this process, and you do not “steal another heap from the process”, you are still occupying physical memory.

+1
source

You are partially right. When the “leaky” process ends, the memory it claims is freed up and becomes available to other processes, but during the whole time of its execution the heap of the leaky process will continue to receive more and more available memory, leaving it less accessible for other processes to store their heaps.

The heap is traditionally dynamically allocated and can grow or shrink on demand.

+1
source

A certain amount of physical memory (RAM) is installed on the computer. The operating system allocates part of this RAM for itself and tracks the rest of the RAM. When a process requests memory, the operating system can allocate memory for this process. Processes cannot access another memory space under normal conditions. However, there is a finite amount of total available memory, so when one of the mallocs / callocs processes stores memory and does not free it, the effect on the system is that less memory is allocated to the operating system for other processes.

+1
source

Source: https://habr.com/ru/post/1203961/


All Articles