Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 545
Vivek Goel Avatar asked Oct 16 '25 15:10

Vivek Goel


1 Answers

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.

like image 57
Don Neufeld Avatar answered Oct 18 '25 08:10

Don Neufeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!