Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .dll files loaded once for every program or once for all programs?

Tags:

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?

like image 707
Nilbert Avatar asked May 17 '10 02:05

Nilbert


People also ask

How are DLL loaded?

DLL files may be explicitly loaded at run-time, a process referred to simply as run-time dynamic linking by Microsoft, by using the LoadLibrary (or LoadLibraryEx ) API function. The GetProcAddress API function is used to look up exported symbols by name, and FreeLibrary – to unload the DLL.

Can you load the same DLL twice?

You can not load the same DLL multiple times into a single process (or not and have any effect). If you make the DLL a COM host and use COM objects then this will be automatically handled by each class instance.

Are all DLL files the same?

DLLs are so much like an EXE that the file format itself is the same.

What program runs DLL file?

The Microsoft Windows Visual Studio is a program that allows you to view, edit and build code into a DLL file.


2 Answers

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.

like image 178
Dean Harding Avatar answered Sep 20 '22 06:09

Dean Harding


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.

like image 40
wallyk Avatar answered Sep 22 '22 06:09

wallyk