DLL size in memory & size on the hard disk

2019-06-20 04:22发布

Is there a relationship between DLL size in memory and size on the hard disk?

This is because I am using Task Manager extension (MS), and I can go to an EXE in the list and right click -> Module, then I can see all the DLLs this EXE is using. It has a Length column, but is it in bytes? And the value (Length) of the DLL seems to be different from the (DLL) size on the hard disk. Why?

5条回答
神经病院院长
2楼-- · 2019-06-20 04:25

The memory footprint will usually be bigger than on disk size because when it is mapped into memory it is page aligned. Standard page sizes are 4KB and 8KB so if your dll is 1KB of code its still going to use 4KB in memory.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-20 04:27

It all depends on what you call "memory", and what exactly does your TaskManager extension show.

Every executable module (Exe/Dll) is mapped into an address space. The size of this mapping equals to its size. And, I guess, this is what your "extension" shows to you.

查看更多
The star\"
4楼-- · 2019-06-20 04:39

There's a relationship, but it's not entirely direct or straightforward.

When your DLL is first used, it gets mapped to memory. That doesn't load it into memory, just allocates some address space in your process where it can/could be loaded when/if needed. Then, individual pages of the DLL get loaded into memory via demand paging -- i.e., when you refer to some of the address space that got allocated, the code (or data) that's mapped to that/those address(es) will be loaded if it's not already in memory.

Now, the address mapping does take up a little space (one 4K page for each megabyte of address space that gets mapped). Of course, when you load some data into memory, that uses up memory too.

Note, however, that most pages can/will be shared between processes too, so if your DLL was used by 5 different processes at once, it would be mapped 5 times (i.e., once to each process that used it) but there would still only be one physical copy in memory (at least normally).

Between those, it can be a little difficult to even pin down exactly what you mean by the memory consumption of a particular DLL.

查看更多
仙女界的扛把子
5楼-- · 2019-06-20 04:43

Don't think of a .dll or a .exe as something that gets copied into memory to be executed.

Think of it as a set of instructions for the loader. Sure it contains the program and static data text. More importantly, it contains all the information allowing that text to be relocated, and to have all its unsatisfied references hooked up, and to export references that other modules may need.

Then if there's symbol and line number information for debugging, that's still more text.

So in general you would expect it to be larger than the memory image.

查看更多
唯我独甜
6楼-- · 2019-06-20 04:47

There are two parts that come into play in determining the size of a dll in memory:

  1. As everyone else pointed out, dll's get memory mapped, this leads to thier size being page aligned (on of the reasons preferred load addresses from back in the day had to be page aligned). generally, page alignment is 4Kb for 32bit systems, 8Kb for 64 bit systems (for a more indepth look at this on windows, see this).
  2. Dll's contain a segment for uninitialized data, on disk this segment is compressed, generally to a base + size, when the dll is loaded and initialized, the space for the .bss segment gets allocated, increasing its size. Generally this a small and will be absored by the page alignment, but if a dll contains huge static buffers, this can balloon its virtualized size.
查看更多
登录 后发表回答