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

A question arose that was similar to this, but without an answer aimed at static, const data (read-only) - the linux system. This is the situation: A shared library is used by many programs in the system. This shared library has a large amount of const data. Will const data be replicated to system memory for every process that links (and uses) a shared library? I understand (or think what I am doing) that the size of the shared library is counted against it at a "high" level, but within Linux Linux will not replace duplicate copies of the executable section. Is this also true for static (namespaces) const data?

+4
source share
1 answer

If the shared object libNNN.so has data in a read-only segment (for example, from the .rodata or .text section), then this segment is mmap -ed from MAP_SHARED to dlopen or ld.so , so there are different processes (loading libNNN.so ) with By using read-only data, they really use the physical RAM containing it.

Static or global read and write data is included in the MAP_PRIVATE read-write mmap -ed segment (from .bss or .data ), so each process has its own (still copied-on-write).

Use objdump to find out which segments are inside libNNN.so

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

note that const things in C often go into .rodata , but with C ++ data built this is no longer the case.

+5
source

Source: https://habr.com/ru/post/1400105/


All Articles