<?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: Tag rubygems</title>
    <link>http://www.stevelongdo.com/articles/tag/rubygems</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>RubyGems and Memory, again...</title>
      <description>&lt;p&gt;I&amp;#8217;ve been toying with switching over to &lt;a href=":http://mephistoblog.com"&gt;Mephisto&lt;/a&gt; and spending some extra time studying memory consumption, which is quite a bit higher in &lt;a href=":http://mephistoblog.com"&gt;Mephisto&lt;/a&gt; than &lt;a href=":http://typosphere.org"&gt;Typo&lt;/a&gt;.  Using the latest svn trunk of &lt;a href=":http://rubygems.org"&gt;RubyGems&lt;/a&gt; (r1100), the bug with multiple &lt;strong&gt;Gem::GemPathSearcher&lt;/strong&gt; instances being created has been fixed, but now there is a different problem.  Multiple &lt;strong&gt;Gem::SourceIndex&lt;/strong&gt; instances are created.  I can see where this might be needed with the recent &lt;em&gt;&amp;#8220;incremental updates to the gem list&amp;#8221;&lt;/em&gt; change that went into &lt;a href=":http://rubygems.org"&gt;RubyGems&lt;/a&gt;, it doesn&amp;#8217;t make sense to have these hanging around in memory for the lifetime of a &lt;a href=":http://rubyonrails.org"&gt;Rails&lt;/a&gt; application.&lt;/p&gt;


	&lt;p&gt;I took the same approach to a fix as last time, adding the &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;include&lt;/code&gt; for &lt;strong&gt;Singleton&lt;/strong&gt; into the &lt;em&gt;SourceIndex.rb&lt;/em&gt; class.  This worked fine for loading up &lt;a href=":http://mephistoblog.com"&gt;Mephisto&lt;/a&gt; and indeed reduced the memory use substantially.&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;r1100 without &lt;strong&gt;Singleton&lt;/strong&gt;  &lt;/td&gt;
			&lt;td&gt; &amp;#8211; &lt;strong&gt;Gem::SourceIndex x2(1062363)&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;r1100 &lt;ins&gt;with&lt;/ins&gt; &lt;strong&gt;Singleton&lt;/strong&gt; &lt;/td&gt;
			&lt;td&gt; &amp;#8211; &lt;strong&gt;Gem::SourceIndex x1(97621)&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;&lt;br /&gt;
I haven&amp;#8217;t checked to see how this change impairs &lt;a href=":http://rubygems.org"&gt;RubyGems&lt;/a&gt; for getting new gems, update, etc. but it seems like there needs to be a different amount of information loaded for bootstrapping the &lt;code&gt;gem&lt;/code&gt; command, versus a running &lt;a href="http://ruby-lang.org"&gt;Ruby&lt;/a&gt; or &lt;a href=":http://rubyonrails.org"&gt;Ruby on Rails&lt;/a&gt; application.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Does the running application &lt;strong&gt;really&lt;/strong&gt; have a need to know the complete dependency hierarchy for &lt;strong&gt;all&lt;/strong&gt; of the installed gems on a system?&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Even if your application did, would you want that information stored into memory from startup to shutdown?&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Tue, 21 Nov 2006 22:47:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:25fbd8e6-7d57-4719-975d-1a4c0c19e050</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/11/21/rubygems-and-memory-again</link>
      <category>rubygems</category>
      <category>rails</category>
      <category>memory</category>
    </item>
    <item>
      <title>Installing a patched RubyGems on TextDrive</title>
      <description>Tying up the loose end called "Installing a patched &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; on &lt;strike&gt;&lt;a href="http://www.textdrive.com"&gt;TextDrive&lt;/a&gt;&lt;/strike&gt; your shared host". Sorry this took me so long to write up it should have been posted on August 13th.  Anyway the full and gory details follow.&lt;br /&gt;&lt;br /&gt;
</description>
      <pubDate>Thu, 31 Aug 2006 03:35:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1526115b-bbdc-4ed5-b64e-884db81d1e81</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/08/31/installing-a-patched-rubygems-on-textdrive</link>
      <category>rubygems</category>
      <category>textdrive</category>
    </item>
    <item>
      <title>RubyGems memory reduction...</title>
      <description>I did gain some insight into the number of &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; instances through my efforts.  They correspond nicely for each piece of &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; that is loaded; ActionMailer, ActiveSupport, ActiveRecord, ActionPack, ActionWebService and Railties. Each of these has the &lt;code&gt;require 'rubygems'&lt;/code&gt; statement in it.  I believe this is why I was seeing multiple instances of &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt;. &lt;br /&gt;
&lt;br /&gt;
As explained before instances of these take up an amount of memory proportional to the number of gems/gem versions in a repository.  Making &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; a Singleton of course, resolves more than one instance being loaded into memory.  This has been submitted as a patch to &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; &lt;a href="http://rubyforge.org/tracker/?func=detail&amp;atid=577&amp;aid=5394&amp;group_id=126"&gt;[ticket #5394]&lt;/a&gt;.
&lt;br /&gt;
&lt;br /&gt;
Installing a patched version of &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; 0.9.0 on &lt;a href="http://www.textdrive.com"&gt;TextDrive&lt;/a&gt; to demonstrate the fix with &lt;a href="http://ww.typosphere.org"&gt;Typo&lt;/a&gt; proved to be the most frustrating part.  I will write a follow up posting on exactly how to do this in the near future.  Hopefully they will release an updated &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; soon!
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.planetargon.com/"&gt;Shared&lt;/a&gt; &lt;a href="http://www.dreamhost.com"&gt;hosts&lt;/a&gt; will definitely want to patch their &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; installs to reduce their memory consumption across all of their &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; users.  &lt;a href="http://www.textdrive.com"&gt;TextDrive&lt;/a&gt; in particular should buy me a present or something :-) 
&lt;br /&gt;
&lt;br /&gt;
I've learned a great deal about the &lt;a href="http://rubylang.org"&gt;Ruby&lt;/a&gt; memory ecosystem by digging into this issue with &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt;.  I got help from some folks on the &lt;a href="http://www.ruby-forum.com/forum/3"&gt;Rails mailing list&lt;/a&gt; but far and away I want to thank &lt;b&gt;Stef Telford&lt;/b&gt; for the help.  Mad props to you for putting up with all of my questions and ramblings!
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Finally some numbers:&lt;/b&gt;
&lt;br /&gt;
Gem::Dependency x330(57415)
&lt;br /&gt;
Gem::Version::Requirement x693(61939)
&lt;br /&gt;
Gem::SourceIndex x1(1632488)
&lt;br /&gt;&lt;b&gt;Gem::GemPathSearcher2&amp;nbsp;x1(1647806)&lt;/b&gt;&amp;nbsp;&lt;b&gt;&lt;--&lt;/b&gt;&amp;nbsp;&lt;b&gt;Gem::GemPathSearcher&amp;nbsp;x5(7901012)&lt;/b&gt;&lt;br /&gt;
Hash x1219(1789928)
&lt;br /&gt;
Gem::Specification x362(1795089)
&lt;br /&gt;
String x113992(3876455)
&lt;br /&gt;
Array x7415(5080185)
&lt;br /&gt;
&lt;b&gt;TOTAL&amp;nbsp;==&amp;nbsp;16967273&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;&lt;--&lt;/b&gt;&amp;nbsp;&lt;b&gt;TOTAL&amp;nbsp;==&amp;nbsp;28496875&lt;/b&gt;
&lt;br /&gt;&lt;br /&gt;
Update:  These memory numbers are lower than actual process size.  They vary by number of Modules loaded as well as Objects counts.  Not all Objects and Modules can be evaluated for memory size.  The new routing code in the latest version of Rails may also account for some of the difference as well.  So grain of salt...</description>
      <pubDate>Sat, 12 Aug 2006 07:00:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:00dbf8e8-5fa9-43f2-9450-d3e6b7e52444</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/08/12/rubygems-memory-reduction</link>
      <category>typo</category>
      <category>rails</category>
      <category>memory</category>
      <category>rubygems</category>
      <category>profiling</category>
    </item>
    <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 dependency management and memory use...</title>
      <description>I've been looking into the nebulous matter of &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; memory management &lt;a href="http://www.stevelongdo.com/articles/2006/08/04/typo-4-0-and-memory-reduction"&gt;lately&lt;/a&gt;, using &lt;a href="http://typosphere.org"&gt;Typo4&lt;/a&gt; as my model app.  In my research thus far I have noticed that several of the highest memory using items are from the &lt;a href="http://www.rubygems.org/"&gt;RubyGems&lt;/a&gt; library.  Particularly the &lt;b&gt;Gem::GemPathSearcher&lt;/b&gt; object.  After running a few days I have picked up five instances of it taking up nearly 8MB of RAM which is odd because the first time I profiled it there were &lt;b&gt;only&lt;/b&gt; four of them.  I am not sure why &lt;a href="http://www.rubygems.org/"&gt;RubyGems&lt;/a&gt; needs to be in memory after the app initially loads, especially the ~1.5MB &lt;b&gt;SourceIndex&lt;/b&gt; and the 352 &lt;b&gt;Gem::Specification&lt;/b&gt; objects for another ~1.6MB.  Seems like wiring of any dependencies could be done once at start up.  I am not sure if this relates specifically to how &lt;a href="http://typosphere.org"&gt;Typo&lt;/a&gt; uses &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; or if all &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; apps incur the memory overhead penalty for &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt;. If any one with knoweldge of the internal functioning of &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; would take the time to educate me on this it would be much appreciated.  Also if someone could explain why &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; pops up at all since all &lt;a href="http://www.typosphere.org"&gt;Typo&lt;/a&gt; dependencies are deployed into the vendor folder.&lt;br /&gt;
&lt;br /&gt;Another one to watch is the slow accumulation of &lt;b&gt;ActiveRecord::ConnectionAdpaters::MysqlColumn&lt;/b&gt;, up to 217 instances in memory.  Not a particularly large memory hit, but they have steadily grown in number over the last few days.  i may swith over to my &lt;a href="http://www.postgresql.org/"&gt;Postgres&lt;/a&gt; database and see if the same thing happens with its connecton adapter.&lt;br /&gt;&lt;br /&gt;
Finally the allocation of &lt;b&gt;Array&lt;/b&gt; objects is interesting.  The number of instances in use has gone down by roughly -2000 instances but the memory use of the remaining &lt;b&gt;Arrays&lt;/b&gt; has gone up by about 2MB.  I need to explode these &lt;b&gt;Arrays&lt;/b&gt; and see what they hold and why they stick around so long.  Partial output of profiling included below:
&lt;br /&gt;
&lt;br /&gt;Class x852(21194)
&lt;br/ &gt;&lt;b&gt;Gem::Version x709(24981)&lt;/b&gt;
&lt;br/ &gt;&lt;b&gt;ActiveRecord::ConnectionAdapters::MysqlColumn x217(35410)&lt;/b&gt;
&lt;br/ &gt;Regexp x1343(52312)
&lt;br/ &gt;&lt;b&gt;Gem::Dependency x309(53743)&lt;/b&gt;
&lt;br/ &gt;&lt;b&gt;Gem::Version::Requirement x662(59168)&lt;/b&gt;
&lt;br/ &gt;&lt;b&gt;Gem::SourceIndex x1(1563056)&lt;/b&gt;
&lt;br/ &gt;&lt;b&gt;Gem::Specification x352(1714117)&lt;/b&gt;
&lt;br/ &gt;Hash x1257(1858255)
&lt;br/ &gt;String x117204(3869923)
&lt;br/ &gt;&lt;b&gt;Gem::GemPathSearcher x5(7901012)&lt;/b&gt;
&lt;br/ &gt;Array x6093(11102839)&lt;/b&gt;
&lt;br /&gt;
&lt;b&gt;TOTAL == 28496875&lt;/b&gt;
&lt;br /&gt;&lt;br /&gt;
Please do share your experiences with &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; or &lt;a href="http://www.rubygems.org"&gt;RubyGems&lt;/a&gt; memory use in the comments.</description>
      <pubDate>Tue, 08 Aug 2006 06:30:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e9ec453d-bde3-40e3-b93d-a56eee868d15</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/08/08/rubygems-dependency-management-and-memory-use</link>
      <category>rails</category>
      <category>rubygems</category>
      <category>typo</category>
      <category>memory</category>
      <category>stats</category>
    </item>
  </channel>
</rss>
