I'm working on a Linux-based program that loads many plug-ins in the form of shared objects. What I want to find out is how much memory each shared object, and all its data structures, take at a certain point in time. Is it possible to do that? I can modify both the main program and the plug-in shared objects if needed.
-
What kind of program are you working on? – Basile Starynkevitch Oct 28 '14 at 11:51
-
It's some sort of data analyzer. – Elektito Oct 28 '14 at 11:51
2 Answers
It is not possible dynamically, since it may happen that a shared object A.so
is dynamically creating at runtime some object data B which is used then destroyed by shared object C.so
So you cannot say that some data like B "belongs to" a particular shared object; you may (and should) have conventions about that. See RAII, rule of three, smart pointers, ....
The point is that the question "how many memory is used by a given library or shared object" has no sense. Memory and address-space are global to the process, so shared by the main program and all shared objects, libraries, plugins...!
You could however use proc(5) to get information about the entire process. From inside the program, read sequentially /proc/self/maps
to get the map of its address space. From outside the program, read /proc/1234/maps
for process of pid 1234.
You might want to use valgrind. Read more about memory management, garbage collection, reference counting. You could view your issues as related to resource management or garbage collection. You might want to use Boehm's conservative garbage collector (if using standard C++ containers, you'll want to use Boehm gc_allocator
, see this). The point is that liveness of some given data is a global property of the program, not of any particular plugin or function. Think about circular references

- 1
- 1

- 223,805
- 18
- 296
- 547
-
Ah, well, I guess it was worth a try! It seems like I should consider manual accounting or something. – Elektito Oct 28 '14 at 11:51
What I want to find out is how much memory each shared object, and all its data structures, take at a certain point in time. Is it possible to do that?
If the program is running and you have its pid you can examing its memory mappings. For example:
% pmap 1234
[...]
00007f8702f6a000 148K r-x-- libtinfo.so.5.9
00007f8702f8f000 2044K ----- libtinfo.so.5.9
00007f870318e000 16K r---- libtinfo.so.5.9
[...]
This doesn't tell you much about the data structures et al though.

- 178,505
- 25
- 365
- 392
-
Notice that `pmap` is just pretty-printing `/proc/1234/maps` – Basile Starynkevitch Oct 28 '14 at 11:56
-
@BasileStarynkevitch I know and presumably everyone who uses it knows to strace it to find out what it is reading. Thanks! – cnicutar Oct 28 '14 at 13:01