问题描述
我有一个简单的小问题,知道的人可以轻松回答,我用谷歌搜索但找不到答案.
I have a simple small question which someone who knows will be able to answer easily, I searched google but couldn't find the answer.
计算机上同时运行着许多程序,我的问题是:当一个程序加载一个 DLL 时,它是实际加载了 DLL 文件还是找到了已加载 DLL 的内存?例如,ws2_32.dll (winsock 2) 是为每个使用winsock 的程序加载,还是加载一次并且所有使用它的程序都使用相同的内存地址来调用函数?
There are many programs running at once on a computer, and my question is: when a program loads a DLL, does it actually load the DLL file or does it find the memory in which the DLL is already loaded? For example, is ws2_32.dll (winsock 2) loaded for every program that uses winsock, or is it loaded once and all programs that use it use the same memory addresses to call the functions?
推荐答案
它加载一次,所有程序共享相同的内存中代码副本.这有点复杂,但对于 DLL 的只读部分(即代码),操作系统加载程序使用一种称为内存映射"的技术将 DLL 映射到进程的地址空间.这些页面只被所有进程加载到物理内存中一次,即使它们可能将页面映射到其虚拟地址空间中的不同地址.
It's loaded once and all programs share the same in-memory copy of code. It's kind of complicated, but for the read-only sections of the DLL (that is, code) the operating system loader uses a technique called "memory mapping" to map the DLL into the process's address space. The pages are only loaded into physical memory once for all processes, even though they may have the page mapped to different address in their virtual address space.
但是,每个进程都有一个单独的数据部分(因此全局变量不会共享 - 除非您明确要求它们)并且它们显然也有一个单独的堆,因此不会共享动态分配的内存.
However, each process has a separate data section (so that global variables are not shared - unless you explicitly ask them to be) and they obviously also have a separate heap so that dynamically-allocated memory is not shared.
这篇关于.dll 文件是为每个程序加载一次还是为所有程序加载一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!