.dll 文件是为每个程序加载一次还是为所有程序加载一次?
我有一个简单的小问题,知道的人可以轻松回答,我搜索了谷歌但找不到答案。
计算机上同时运行许多程序,我的问题是:当程序加载DLL时,它实际上加载DLL文件还是找到已经加载DLL的内存?例如,ws2_32.dll(winsock 2)是为每个使用winsock的程序加载的,还是加载一次并且使用它的所有程序都使用相同的内存地址来调用函数?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它加载一次,所有程序共享相同的内存中代码副本。这有点复杂,但对于 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 是为代码和数据的共享使用而准备的:大多数 Windows 环境都尊重共享性(通过将代码的相同内存副本映射到每个进程的内存空间)以节省内存。
然而,“加载”操作的一部分(从进程的角度来看)正在运行 DLL 的初始化:这是在每个进程中单独完成的,具有每个进程私有的数据区域的不同副本。
It depends on what you mean by "loaded".
The DLL is prepared for shared use of code and data: most Windows environments honor the shareability (by mapping the same memory copy of the code into each process's memory space) to conserve memory.
However, part of the "load" operation (from a process's point of view) is running the DLL's initialization: that is done separately in each process with distinct copies of the data areas which are private to each process.