.dll 文件是为每个程序加载一次还是为所有程序加载一次?

发布于 2024-09-01 18:02:18 字数 182 浏览 7 评论 0原文

我有一个简单的小问题,知道的人可以轻松回答,我搜索了谷歌但找不到答案。

计算机上同时运行许多程序,我的问题是:当程序加载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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2024-09-08 18:02:18

它加载一次,所有程序共享相同的内存中代码副本。这有点复杂,但对于 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.

无所的.畏惧 2024-09-08 18:02:18

这取决于您所说的“已加载”是什么意思。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文