Overhead of using a shared library


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Overhead of using a shared library
# 1  
Old 05-25-2010
Overhead of using a shared library

Hi,
I found a very strange thing when I linked my executable with a shared library. That is the executable only references a small function of the shared library, and the size of this function is only hundred bytes, but when I check the /proc/pid/smaps, I found that the 'Rss' of this shared library is 76 KB that is much larger than the actual size of the referenced function. I just wonder why it is. What's the extra data in this 76 KB of memory? Thanks in advance.

Code:
00c4e000-00d73000 r-xp 00000000 00:15 38896026   /home/xhbf63/Desktop/libnewshared.so
Size:               1172 kB
Rss:                  76 kB
Pss:                  76 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:        76 kB
Private_Dirty:         0 kB
Referenced:           76 kB
Swap:                  0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB



---------- Post updated at 08:34 AM ---------- Previous update was at 06:37 AM ----------

Can anybody tell me the reason???

---------- Post updated 05-25-10 at 07:15 AM ---------- Previous update was 05-24-10 at 08:34 AM ----------

Cheers!!!

Last edited by Scott; 05-24-2010 at 08:55 AM.. Reason: Code tags
# 2  
Old 05-25-2010
Shared libraries have a hashtable (an index) and a symbol table, both of which have to be present/locatable in memory before dl_open can access one of the members. RSS is a process resident header for that rtl library's shared memory segment. Each shared memory segment has one.

In Linux, rss is part of the task struct in the kernel, an unsigned long.

If your system has pmap try
Code:
pmap -x -a [pid of your process]

And you will see that all of the shared memory segments have an rss, it is part of using shared libraries. And shared memory.

Are you running in an ARM or similar environment? If so, consider linking your tiny function into your code statically.

Last edited by jim mcnamara; 05-25-2010 at 12:45 PM..
# 3  
Old 05-25-2010
A old but good paper to read is R Gingell et al's seminal paper Shared Libraries in SunOS. See Section 7 for a real world example.
# 4  
Old 05-25-2010
Thanks so much for your reply!!!
You said that a shared library has a hash table and a symbol table. Do you mean the .hash and .dynsym sections of a shared library? And these two sections must be loaded in the memory before actual codes are referenced. Are there any other sections of a shared library must be loaded before actual reference? such as .dynstr, .plt, .got, etc..
Another question is that all these sections must be loaded entirely or just a portion of them.

Actually I got the above smaps from my test programs. I wrote a test shared library contains 20000 functions and a executable only references one function of the shared library (I do not know if these test programs are appropriate) The following is the output of section information of the test library:
Code:
...
Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .note.gnu.build-i NOTE            000000f4 0000f4 000024 00   A  0   0  4
  [ 2] .hash             HASH            00000118 000118 023918 04   A  4   0  4
  [ 3] .gnu.hash         GNU_HASH        00023a30 023a30 027910 04   A  4   0  4
  [ 4] .dynsym           DYNSYM          0004b340 04b340 04e290 10   A  5   1  4
  [ 5] .dynstr           STRTAB          000995d0 0995d0 02e24b 00   A  0   0  1
  [ 6] .gnu.version      VERSYM          000c781c 0c781c 009c52 02   A  4   0  2
  [ 7] .gnu.version_r    VERNEED         000d1470 0d1470 000020 00   A  5   1  4
  [ 8] .rel.dyn          REL             000d1490 0d1490 000020 08   A  4   0  4
  [ 9] .rel.plt          REL             000d14b0 0d14b0 000010 08   A  4  11  4
  [10] .init             PROGBITS        000d14c0 0d14c0 000030 00  AX  0   0  4
  [11] .plt              PROGBITS        000d14f0 0d14f0 000030 04  AX  0   0  4
  [12] .text             PROGBITS        000d1520 0d1520 018798 00  AX  0   0 16
...

You can see that the .hash and .dynsym sections are quite large. Then I ran the executable and read the smaps (as shown previously) of the process, the Rss of the test shared library was much smaller than the size of hash table or symbol table. Does it mean that only a portion of these sections are loaded in the memory?
# 5  
Old 05-25-2010
No I was not clear, sorry.

Those RSS entries are unsigned long words that point to memory that is resident in the process - resident set size. That ememory is local to the process. Those entries are used in part to access (map to) non-resident shared memory segments. They may also be process-resident data.

I did not mean to imply the symbol table/hashtable was actually in the process resident shared memory segment. If that were true then all processes running libc would have a huge rss segment just for the symbol/hash tables.


Sample pmap -x -a output
Code:
 pmap -x -a 21317
21317:  ./uzpplpl uimsusr u_pick_it 16106 9387 10107 10001
 Address  Kbytes     RSS    Anon  Locked Mode   Mapped File
00010000      56      56       -       - r-x--  uzpplpl
0002C000     208     208      96       - rwx--  uzpplpl
00060000    9320    4232    4232       - rwx--  uzpplpl
0097A000    1280    1160     760       - rwx--    [ heap ]
FE400000   10408    4160       -       - r-x--  libclntsh.so.9.0
FEE38000     360     360     144       - rwx--  libclntsh.so.9.0
FEE92000      56      16      16       - rwx--  libclntsh.so.9.0
FEFB0000      16      16       -       - rw---    [ anon ]
FEFC6000       8       8       -       - rwxs-    [ anon ]
FEFD0000      16      16       8       - rw---    [ anon ]
FEFE0000      96      88       -       - r-x--  libthread.so.1
FF008000       8       8       8       - rwx--  libthread.so.1
FF00A000       8       8       -       - rwx--  libthread.so.1
FF020000       8       8       -       - r-x--  libmd5.so.1
FF032000       8       8       -       - rwx--  libmd5.so.1
FF040000      40      40       -       - r-x--  libaio.so.1
FF05A000       8       8       -       - rwx--  libaio.so.1
FF060000       8       8       -       - r-x--  libwtc9.so
FF070000       8       8       -       - rwx--  libwtc9.so
FF080000     688     688       -       - r-x--  libc.so.1
FF13C000      24      24      16       - rwx--  libc.so.1
FF142000       8       8       8       - rwx--  libc.so.1

Note the relatively small size of the segments.
# 6  
Old 05-25-2010
well, if the resident shared memory does not contain hash table or symbol table, what else does it contain except the pure code referenced by the executable? As I mentioned before, the size of the referenced function is only hundred bytes, but the size of resident shared memory is 76 KB. So there must be other sections loaded in the physical memory when use a share library, right?
# 7  
Old 05-25-2010
Before we can fully answer that question you would have to disclose how you build your shared library, the source code of the function you are calling, whether you used lazy loading, etc. what OS and platform you are on, what compiler you are using, what linker you are using, what loader you are using, and lots more.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Add shared members from library to same library in a different directory

I'm trying to install libiconv to AIX 7.1 from an rpm off of the perzl site. The rpm appears to install but I get this error message. add shr4.o shared members from /usr/lib/libiconv.a to /opt/freeware/lib/libiconv.a add shr.o shared members from /usr/lib/libiconv.a to ... (5 Replies)
Discussion started by: kneemoe
5 Replies

2. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

3. Programming

Loading the shared library I want

Hi All I have been given by someone else header file and a shared library to be used by my C++ application. Compilation is fine but when I try to executes the application I receive the following error. ./first: error while loading shared libraries: libMyLib.so.9: cannot open shared object file:... (2 Replies)
Discussion started by: manustone
2 Replies

4. Shell Programming and Scripting

How to change a Makefile from building static library to shared library?

Hi: I have a library that it only offers Makefile for building static library. It built libxxx.a file. How do I in any way build a shared library? (either changin the Makefile or direct script or command to build shared library) Thanks. (1 Reply)
Discussion started by: cpthk
1 Replies

5. HP-UX

Shared Library Issue HP UX

I have never seen this issue before, but here is what is happening. I link an executable against two dynamic mlib libraries veclib and lapack. We place a newer version of these libraries in a write only directory and point the shlib_path at that directory. When the executable runs, it gets a... (3 Replies)
Discussion started by: sambarusty
3 Replies

6. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

7. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

8. UNIX for Advanced & Expert Users

shared library

What is the primary difference between static library and dynamic library? and how to write static shared library? (1 Reply)
Discussion started by: areef4u
1 Replies

9. HP-UX

Shared Library Problem

I have this error when I try to do check on the oracle database... Can you help me figure out whats the problem? Thanks for all the help! /usr/lib/pa20_64/dld.sl: Unable to find library 'libjox8.sl'. /usr/lib/pa20_64/dld.sl: Unable to find library 'libjox8.sl'. ... (1 Reply)
Discussion started by: vinz
1 Replies

10. Programming

Shared Library

hello all I want to work in shared libraries how can i work in Linux Environment ? (2 Replies)
Discussion started by: rajashekaran
2 Replies
Login or Register to Ask a Question