Search code examples
c++forkdynamic-library

Will using shared library in place of static library effect memory usage?


I am linking against 10 static library.

My binary file size is getting reduced when I am using dynamic library.

As I know using dynamic library will not reduce memory usage.

But my senior told me that using shared library will also reduce memory usage ? (when multiple process are running for the same executable code. )

Is that statement is right ?

he told me that as there will no duplicate copy of function used in library , so memory usage will be less. when you create n instance of that process.

When the process start it fork it's 10 children. So will using dynamic library in place of static library reduce total memory usages ?


Solution

  • In your example, dynamic libraries won't save you much. When you fork your process on a modern OS all the pages are marked copy on write rather than actually copied. So your static library is already shared between your 10 copies of your process.

    However, where you can save is when the dynamic library is shared between different processes rather than forks of the same process. So if you're using the same glibc.so as another process, the two processes are sharing the physical pages of glibc.so, even though they are otherwise unrelated processes.