views:

376

answers:

3

Hi, I have a question on shared libraries vs static libraries loading time.

Assume that i have a executable foo.exe which uses liba, libb, libc. Also at a given time there are more than 10 instances of the executable running on the machine.

Now if the above 3 libraries were shared libraries : 1st Insance is loaded into RAM : The time taken will be time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc 2nd instance is started : Now assume a second instance of this executable is run. Since all libraries are already loaded into the main memory, time taken is only for loading the main() to the memory which is negligible .

Now if the above 3 libraries were static libraries : 1st Insance is loaded into RAM : The time taken will be time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc (Offcourse its now all part of the executable as a whole) 2nd instance is started : Now assume a second instance of this executable is run. Time taken will be again time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc. (Since each executable cant share librareies as these are static librareies)

So my conclusion is that with static library the loading time will be more. But i was told that shared libraries take more time during loading than static libraries, so there will be a delay and so shared libraries is not a good option. How is this possible ?

+2  A: 

Static libraries are linked at compiled time, shared libraries are linked at runtime. Therefore, executables using static libraries amortize all their link time before even being written to disk.

Ignacio Vazquez-Abrams
If there are 3 libraries liba, libb, libc all are static lib. Now two executables foo1.exe, and foo2.exe which use all 3 lib. 1. If multiple instances of foo1.exe are run they will share the text pages. Is this right ?2. If foo1.exe, and foo2.exe are run at same time they will not share the text pages. Is this right ?Pls let me know
sud
In the first case, the text pages shared will be that of foo1 and not of the actual libraries since the libraries no longer exist after static linking. Otherwise, both are correct.
Ignacio Vazquez-Abrams
+2  A: 

Linking (resolving references) is not free. With static linking, the resolution is done once and for all when the binary is generated. With dynamic linking, it has to be done every time the binary is loaded. Not to mention that code compiled to run in a shared library can be less efficient than code compiled to be linked statically. The exact cost depends on the architecture and on the system's implementation of dynamic linking.

The cost of making a library dynamic can be relatively high for the 32-bit x86 instruction set: in the ELF binary format, one of the already scarce registers has to be sacrificed to make dynamically linked code relocatable. The older a.out format placed each shared library at a fixed place, but that didn't scale. I believe that Mac OS X has had an intermediate system when dynamic libraries where placed in pre-determined locations in the address space, but the conflicts were resolved at the scale of the individual computer (the lengthy "Optimizing system performance" phase after installing new software). In a way, this system (called pre-binding) allows you to have your cake and eat it too. I do not know if prebinding is still necessary now that Apple pretty much switched to the amd64 architecture.

Also, on a modern OS both statically and dynamically linked code is only loaded (paged in) from disk if it is used, but this is quite orthogonal to your question.

Pascal Cuoq
Thanks a lot for this unbelievably fast response, we have 2 architecural scenarious i have given my question as a answer to my question pls find it below.
sud
A: 

Thanks a lot for this unbelievably fast response. we have 2 architecural scenarious :

Q1. Architecure-1 :Assume exe size 3GB (static libraries). 95% is libraries and 5% main().With such a huge size would loading of this exe take more time (assuming static libraries) or would linking of this exe take more time (assuming using shared libraries, and if all libraries are already in memory only linking has to be done.)

Architecure-2 :Assume i have a exe size 1.5GB (95% lib + 5% main()), and 6 instances of this are running at the same time. Once these 6 instances are up they will run for days, assume we are ready to take the extra delay during initial loading+linking of these 6 instances.

Q2. Now if i am using shared objects rather than static objects, will i have lot of free space on the RAM since all the libraries are shared amongst the 6 instances ? will not my realtime execution speed up because of more space in RAM which decrease the page swapping ?

Q3. If i use 'map' files to decrease the number of symbols exported (which is only possible using shared libraries) will not my symbol table size decrease and will it not improve the runtime performance ?

Thanks Sud

sud
If you mean running instances of the same program ("foo.exe" in your original question), the code will be shared in memory (with all the advantages) with the statically linked version just the same as with the dynamically linked one.
Pascal Cuoq
Only if you meant slightly different programs foo1.exe, foo2.exe each using the same liba, libb, lic would there be an advantage to dynamic linking. In this case only it would allow sharing something that could not be shared with static linking.
Pascal Cuoq
Actually i meant to say that the same executable foo.exe (using static lib) has 6 instances instead of foo1, foo2. But in this case how will the code be shared in memory ? Wouldnt each instance has its own copy in the RAM ? I thought only with shared libraries it will be shared, not with static lib. Can you provide a url which could enlighten me.
sud