This paragraph is motivation. While my question is not Rails-specific, I am 
asking it because of Rails. I've been investigating the memory footprint of 
my Mongrels. It is nice that they share the .so libraries from ImageMagick 
as well as other C libraries. However, each one still has about 20MB in 
[heap]. My theory is that a lot of this is coming from ActiveRecord and 
friends getting loaded again and again for each Mongrel, which seems to me 
entirely unnecessary. My "marginal cost of Apache" is 1376kB. My "marginal 
cost of Mongrel" is 27528kB, with the code I wrote. It seems that the latter 
could be reduced a lot by sharing some Ruby libraries.

The question is as follows: if I require 'library' in one instance of Ruby 
and then require 'library' again in another instance of Ruby, then do I get 
duplicate copies of library's code in two chunks of my RAM? (I'm thinking I 
do.) Why?


For further details and perhaps clarification, consider the following 
script:

require 'smaps_parser'

smaps = SmapsParser.new(Process.pid)
puts smaps.sums.inspect

%w{rubygems active_record action_controller action_view RMagick}.each do |l|
  puts "\nRequiring #{l}."
  require l
  smaps.refresh
  puts smaps.sums.inspect
end


Though my Mongrel processes have already (each?) loaded copies of each l, 
and though there is nothing "private" about the code in each l, I get the 
following output, in which one should pay particular attention to the 
increase of [:private_dirty]:

{:rss=>1520, :shared_clean=>964, :shared_dirty=>0, :private_clean=>12, 
:size=>2968, :private_dirty=>544}

Requiring rubygems.
{:rss=>5032, :shared_clean=>1676, :shared_dirty=>0, :private_clean=>224, 
:size=>7476, :private_dirty=>3132}

Requiring active_record.
{:rss=>12920, :shared_clean=>1816, :shared_dirty=>0, :private_clean=>224, 
:size=>15452, :private_dirty=>10880}

Requiring action_controller.
{:rss=>18680, :shared_clean=>1828, :shared_dirty=>0, :private_clean=>228, 
:size=>21152, :private_dirty=>16624}

Requiring action_view.
{:rss=>21088, :shared_clean=>1828, :shared_dirty=>0, :private_clean=>228, 
:size=>23524, :private_dirty=>19032}

Requiring RMagick.
{:rss=>22512, :shared_clean=>2660, :shared_dirty=>0, :private_clean=>228, 
:size=>29792, :private_dirty=>19624}