<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Musings of a Trained Monkey: RubyGems: Single/Multi-tons and memorysize</title>
    <link>http://www.stevelongdo.com/articles/2006/08/09/rubygems-single-multi-tons-and-memorysize</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>RubyGems: Single/Multi-tons and memorysize</title>
      <description>There is a line in the &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; source code, &lt;code&gt;@gempath_searcher ||= Gem::GemPathSearcher.new&lt;/code&gt;, from custom_require.rb that seeks to make &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; a Singleton class. For some reason this isn't sufficient as I have seen as many as five instances of it existing in the same thread during profiling of &lt;a href="http://www.typosphere.org"&gt;Typo&lt;/a&gt;.  I am not sure why more than one gets generated. It may relate to &lt;a href="http://www.rubyonraiils.org"&gt;Rails&lt;/a&gt; aliasing the &lt;code&gt;Kernel.require&lt;/code&gt; method after &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; has in custom_require.rb.
&lt;br/&gt;&lt;br/&gt;
Review of the source makes me understand why a &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; instance can be so large, it holds the gem specifications for every gem and version on the load path.  Running locally this is not a big deal, as you probably don't have a lot of gems and their versions installed.  On a shared host though there are tons of gems and versions of gems installed (&lt;b&gt;350+&lt;/b&gt; on my TextDrive server!).
&lt;br/&gt;&lt;br/&gt;
A permanent solution to fix &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; to truly be a Singleton class would probably be something as simple as checking &lt;code&gt;Object.const_defined?(:GemPathSearcher)&lt;/code&gt; to see if the class is already in memory.
&lt;br/&gt;&lt;br/&gt;
A quick alternative would be restricting the gem library path to a gem repository other than the shared host's repository.  This can be done by exporting the &lt;b&gt;GEM_HOME&lt;/b&gt; variable in your shell.  There is also a &lt;b&gt;GEM_PATH&lt;/b&gt; variable that can be used to support multiple repositories.  Examples of how to work with these variables can be found on the &lt;a href="http://wiki.rubyonrails.org/rails/pages/HowToUseMultipleGemRepositories"&gt;Rails wiki&lt;/a&gt;.  It should be possible to make a repository containing just the gems your app needs and nothing else.  This will reduce the memory footprint of &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; quite a bit, no matter how many instances of them there might be!
 &lt;br /&gt;&lt;br /&gt;
Obviously I am still learning the implications of &lt;a href="http://rubygems.org"&gt;RubyGems&lt;/a&gt; working together with &lt;a href="http://rubyonrails.org"&gt;Rails&lt;/a&gt; apps.  I apologize for you the reader being dragged along on my quest to learn.  Anyone wishing to bring this quest to a quick end just tell me how it ends.
&lt;br /&gt;&lt;br /&gt;
&lt;strong&gt;Update:&lt;/strong&gt; I will continue my research until we arrive at the truth.  I still have some other &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; optimizing techniques to apply.  My hope is using something like the gemconfigure file described &lt;a href="http://www.rubygems.org/read/chapter/4#page73"&gt;in the documentation&lt;/a&gt; will be the difference between the Whole Milk Memory and Skim Milk Memory.  Also I will reformat the code in the post to be pretty tomorrow.  need to add some stuff to scribbish to accomplish this and I want to go to bed now...</description>
      <pubDate>Wed, 09 Aug 2006 03:41:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3df63391-1cf6-4237-b987-aa0823be01ac</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/08/09/rubygems-single-multi-tons-and-memorysize</link>
      <category>rubygems</category>
      <category>rails</category>
      <category>typo</category>
      <category>memory</category>
      <category>profiling</category>
      <category>howto</category>
    </item>
    <item>
      <title>"RubyGems: Single/Multi-tons and memorysize" by Matthew Margolis</title>
      <description>Great to hear that someone is looking into this.  I look forward to your findings.</description>
      <pubDate>Thu, 10 Aug 2006 14:33:32 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a1d596b9-9ef5-4faa-91c1-669457869f85</guid>
      <link>http://www.stevelongdo.com/articles/2006/08/09/rubygems-single-multi-tons-and-memorysize#comment-153</link>
    </item>
    <item>
      <title>"RubyGems: Single/Multi-tons and memorysize" by Piers Cawley</title>
      <description>Yikes! That explains a whole lot.</description>
      <pubDate>Wed, 09 Aug 2006 05:13:58 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ff50dae0-e875-4a6b-a58d-2656b591ceec</guid>
      <link>http://www.stevelongdo.com/articles/2006/08/09/rubygems-single-multi-tons-and-memorysize#comment-152</link>
    </item>
  </channel>
</rss>
