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

﹥>﹥吖頭↗ 提交于 2019-12-09 11:16:56

问题


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.


回答1:


  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.




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/7252883/do-different-programs-gets-their-memory-from-a-common-heap-or-from-a-separate-he

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!