blog dds

2007.10.06

The Memory Savings of Shared Libraries

A recent thread in the FreeBSD ports mailing list discusses the benefits and drawbacks of static builds. How can we measure the memory savings of shared libraries?

Here is a Perl script I used for providing concrete data for the benefits of shared libraries in my book Code Quality.

#!/usr/bin/perl

# Map from .so inodes to file names
open(IN, q{locate .so | grep -v obj | xargs ls -li | grep -v -e '->'|}) || die;
while (<IN>) {
	@F = split;
	$fname{$F[0]} = $F[9];
}

# Get a list of all shared objects
open(IN, q{fstat -m | grep mmap | sed 's/rw$/ r/' | sort -u|}) || die;
while (<IN>) {
	@F = split;
	if (defined($fname{$F[5]})) {
		$used{$fname{$F[5]}}++;
		$size{$fname{$F[5]}} = $F[7];
		$unshared += $F[7];
		$mmap++;
	}
}

for $fname (keys %size) {
	$ssize += $size{$fname};
	$nobj++;
}

print "$nobj shared objects sharing $ssize of memory with $mmap mappings.
Without shared libraries $unshared bytes would be needed.\n";
I rerun today the script on a web server running FreeBSD 6.2, and I found 98 shared objects sharing 16,790,901 bytes of memory through 1,002 mappings. Without shared libraries the corresponding binaries would require 198,815,270 bytes - an order of magnitude more memory.

On the FreeBSD project shell login server I found 58 shared objects sharing 11,285,262 bytes of memory through 2,127 mappings. Without shared libraries the corresponding binaries would require 515,107,268 bytes - 50 times more.

These are not just memory savings, but, more importantly on a modern system, they contribute to improved locality in the code cache.

Read and post comments    AddThis Social Bookmark Button


Creative Commons License Last modified: Saturday, October 6, 2007 9:16 pm
Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.