Why don't Ruby libraries share memory?



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}


.



Relevant Pages

  • Re: Why dont Ruby libraries share memory?
    ... I've been investigating the memory footprint of ... It is nice that they share the .so libraries from ImageMagick ... cost of Mongrel" is 27528kB, ... I suppose the main problem is that Rails (or ActiveRecord, ...
    (comp.lang.ruby)
  • Re: Why dont Ruby libraries share memory?
    ... It is nice that they share the .so libraries from ImageMagick ... cost of Mongrel" is 27528kB, ... if I require 'library' in one instance of Ruby ... I suppose the main problem is that Rails (or ActiveRecord, ...
    (comp.lang.ruby)
  • Re: Why dont Ruby libraries share memory?
    ... It is nice that they share the .so libraries from ImageMagick as well as other C libraries. ... 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. ... 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? ... Good workers build better tools. ...
    (comp.lang.ruby)
  • RE: 2 questions: Requiring amd Linking Document Titles
    ... "greg" wrote: ... > I found this in the wss FAW for requiring title ... > Which ONET.XML should I edit? ... > How do I make Title a compulsory field for all new document libraries? ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Way to many calls to Dir#[]
    ... Tj Holowaychuk wrote: ... unnecessarily slowing things down. ... libraries I am requiring, and none of them produce this same number of ...
    (comp.lang.ruby)