views:

74

answers:

1

Assume a executable foo.exe is built based on static libraries and 6 instances of this foo.exe are running at the same time on the a machine. Since all the code is same (read only part) except for the read write part on the RAM, will there be any sharing amongst these 6 instances in the RAM to improve performance ?

I do know that if the above foo.exe uses shared libraries instead ,even though there are 6 instances running only 1 instance of these shared libraries will be in the RAM.

+2  A: 

It depends on the OS.

For Linux and Solaris, all the instances will definitely share the memory pages that hold the code (or text as it is properly called).

They may also share data pages that originate from the executable (i.e. for global and static data). What happens is those pages are shared with a technique called copy-on-write or COW. As long an instance does not modify the data pages, they will be shared. But once a instance modifies a data page it will get it's own copy.

I'm guessing that modern versions of Windows does the same thing, but I don't know for sure.

R Samuel Klatchko
Thank you for the response. But if the text pages are being shared then what special advantage do we have with shared lib, compared to static lib ? Is there any doc/url you can point me to to get more info on this.
sud
Shared libraries also share memory when used by multiple executables. If both foo.exe and bar.exe link to shared library X, shared library X will only be loaded in memory once. But if foo.exe and bar.exe link to static library Y, there will be no sharing between the process running foo and the process running bar.
R Samuel Klatchko
@solotim - if there is a specific issue you are confused about, I would be glad clarify.
R Samuel Klatchko
@Klatchko: You said on linux/solaris, the memory pages that hold the library code are shared between different processes regardless the library is .a or .so, do I understand the right way? As for your foobar example, you said no sharing occurs for static lib Y. I think those two statements are contradictory to each other. I'm confused at this point.
solotim
@solotim - the difference is singe executable being run multiple times simultaneously vs different executables being run at the same time. For a single executable being run multiple times, all processes share all the code pages (including the code coming from a static library). But if there are multiple executables (foo.exe and bar.exe) which both link with the libhelper static library, the code from libhelper in foo.exe will not share the same pages as the code from libhelper in bar.exe.
R Samuel Klatchko
@Klatchko: Thank you very much. I did think wrong.
solotim