Do different programs get their memory from a common heap or from a separate heap?

I'm a little confused how glibc on linux allocates its memory for various programs. These are a few questions:

  • Is it allocated from a common heap (i.e. there is a common heap across all processes in Linux) or is there one allocated heap for each process in the system.

  • Also, if I compile one static library and, finally, are statically bound to the main process, how will it get its memory? This is already connected with some other heap (as we already compiled it) or will get its memory from the main heap of the process.

+6
source share
3 answers
  • In the sense of libc there is no common heap - this violates the rules for protecting processes and the rules of virtual memory. Each process saves its own heap. The kernel (using the MMU in the processor) supports virtual memory tables that map virtual addresses in real memory.

  • Static libraries are nothing more than linking code at compile time - there is no concept of the runtime of a static library. This is the same as the process, and will use its heap.

+4
source

The heap (and any other writable memory - stack, BSS, etc.) is separate for each process. As part of the process, memory may be shared between threads, and may not be so (in the case of local thread storage). This is true for newly created applications. For a fork -ed application, memory is shared until one of them writes (copy-on-write).

Any memory with constant access (for example, a shared library or running the same application several times) is likely to be shared between processes. This solution is for an executable kernel loader.

The static library is directly linked to the executable file, so for each executable file there is a separate copy (except for several instances of the same executable file).

0
source

Each process has its own virtual heap. However, it can exchange physical RAM or not, depending on access. See copy-on-write for details.

0
source

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


All Articles