John
John

Reputation: 2326

Do/Should Shared Libraries (libNNN.so) Share Const Data?

There was a question similar to this one, but w/o the answer targeted at static, const data (read only data) an a linux system. This is the situation: A shared library is being used by many programs on the system. That shared library has a large amount of const data. Will that const data be replicated in system memory for each process that links to (and uses) the shared library? I understand (or think I do) that the size of the shared library is counted against it at a "high" level, but that under the covers Linux will not swap out duplicate copies of the executable section. Is this also true for the static (namespace level) const data?

Upvotes: 5

Views: 371

Answers (1)

If the shared object libNNN.so has data in a read only segment (e.g. from a .rodata or .text section), then that segment is mmap-ed with MAP_SHARED by dlopen or ld.so so different processes (loading that libNNN.so) using that read-only data are indeed sharing the physical RAM containing it.

The static or global read-write data is going into a MAP_PRIVATE read-write mmap-ed segment (from .bss or .data sections) so each process has its own (still copied-on-write).

Use objdump to find out what segments are inside a libNNN.so

Use pmap or /proc/1234/maps to learn about the memory mapping of process of pid 1234. (From inside the application, read sequentially /proc/self/maps if needed, or /proc/self/statm if you want to measure).

notice that const things in C often go into .rodata, but with C++ constructed data this is no more the case

Upvotes: 6

Related Questions