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";
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