10

I am a bit confused how glibc on linux allocates its memory to various program.These are the few questions:

  1. Is it been allocated from a common heap(i.e is there a common heap across all of the processes in linux) or is there one heap allocated for every process in the system.

  2. Also suppose if I am compiling one static library and it finally gets statically linked to the main process, how it will get its memory? Is it already linked with some other heap(as we already compiled it) or will gets its memory from the main process's heap.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
pjain
  • 1,149
  • 2
  • 14
  • 28
  • 1
    Here's an answer that illustrates the Linux Memory Model: http://stackoverflow.com/questions/2048007/linux-ia-32-memory-model – Daniel Pereira Aug 31 '11 at 05:33
  • 1
    See [How The Kernel Manages Your Memory](http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory) for an overview ... and then some! So much for being "simple" ;-) –  Aug 31 '11 at 05:37

3 Answers3

5
  1. There is no common heap in the libc sense - this would violate process protection and virtual memory rules. Each process maintains its own heap. The kernel (with the help of the MMU in the processor) maintains the virtual memory tables which map virtual addresses to real memory.

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

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • from your point 2)-Does it mean that a shared memory will have its own memory and it is different from the heap used by the main process of the system ? – pjain Aug 31 '11 at 06:09
  • Shared libraries will use the heap of the process, but their code will be shared amongst processes. – Yann Ramin Aug 31 '11 at 06:47
  • and what about the dynamic library.They also share the heap from the main process or have different heap for them. – pjain Aug 31 '11 at 07:53
  • got my answer at this http://stackoverflow.com/questions/1178127/shared-libraries-memory-space.Thanks all for your support. – pjain Aug 31 '11 at 10:52
  • I understand virtual memory and the memory tables in the kernel. But each process has its own heap? Imagine the default heap is 4mb at maximum. Will each process use those 4mb even if they don't allocate anything at all, or will the kernel control what which process allocate? Is the heap similar to the stack, in relation to each process having its instance? – Spidey Feb 13 '13 at 18:57
  • @Spidey: absolutely. Its guaranteed to be unique. Note that even if you ask for and get, say 4MB, modern systems may not actually commit that much memory. You can read a lot about memory overcommit. – Yann Ramin Feb 14 '13 at 20:28
0

The heap (and any other writable memory - stack, BSS, etc) is separate for each process. Within the process the memory might be shared between threads, and it might not be (in case of thread local storage). This is true for newly created applications. For fork-ed application, the memory is shared until either process writes to it (copy-on-write).

Any read-only memory (like a shared library or running the same application multiple times) will probably be shared between the processes. This is a decision for the kernel executable loader.

A static library is linked directly to the executable, so there is a separate copy for each executable running (unless it's multiple instances of the same executable).

Eli Iser
  • 2,788
  • 1
  • 19
  • 29
0

Each process has its own virtual heap. It may, however, share physical RAM, or not, depending on access. See copy-on-write for some more background.

Keith
  • 42,110
  • 11
  • 57
  • 76