Where is dynamically allocated memory located?

We know that the malloc() and new operations allocate memory from the heap dynamically, but where is the heap? Each process has its own heap in the namespace for dynamic allocation, or the OS has a global one common to all processes. What else, I read from the textbook that as soon as a memory leak occurs, the missing memory cannot be reused until the next restart of our computer. Is this thesis correct? If so, how can we explain this?

Thanks for your reply.

Sincerely.

+4
source share
3 answers

The memory is allocated from the address space of the user virtual process memory. And all the memory is restored by the OS at the end of the process, there is no need to restart the computer.

+8
source

Typically, the C runtime will use various OS APIs to allocate memory, which is part of the process address space. Within the allocated memory, it creates a heap and allocates memory from this heap through calls to malloc or new .

The reason for this is that often the OS APIs are finely grained and require you to allocate memory in large fragments (such as page size), while your application usually wants to allocate small amounts of memory at any given time.

+1
source

You do not mention the OS of interest. This certainly does not mean a direct answer.

Try looking at a book about OS such as Tanenbaum’s

0
source

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


All Articles