<?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>Segment7: Controlling Rails Process Size</title>
    <link>http://blog.segment7.net/articles/2006/09/13/controlling-rails-process-size</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The Blog</description>
    <item>
      <title>Controlling Rails Process Size</title>
      <description>&lt;p&gt;Now that Ruby 1.8.5 is out setting process limits is easier than ever before!  We used to use a small shell script run from cron to kill processes that got too big for their britches.  Unfortunately this was difficult to do both well and simply (we chose simply).

&lt;p&gt;Now, in Ruby 1.8.5, we have Process::setrlimit:

&lt;pre&gt;&lt;samp&gt;$ ri Process::setrlimit
----------------------------------------------------- Process::setrlimit
     Process.setrlimit(resource, cur_limit, max_limit)        =&gt; nil
     Process.setrlimit(resource, cur_limit)                   =&gt; nil
------------------------------------------------------------------------
     Sets the resource limit of the process. cur_limit means current 
     (soft) limit and max_limit means maximum (hard) limit.

     If max_limit is not given, cur_limit is used.

     resource indicates the kind of resource to limit. The list of 
     resources are OS dependent. Ruby may support following resources.

Process::RLIMIT_COREcore size (bytes) (SUSv3)
[...]
Process::RLIMIT_RSSresident memory size (bytes) (4.2BSD, GNU/Linux)&lt;/samp&gt;&lt;/pre&gt;

&lt;p&gt;There's a bunch more in there, but those were the two I was most interested in using.  I really don't want cores, and I don't want my application processes to grow too big, but I want cron jobs to get as big as they need to get.

&lt;p&gt;At the top of &lt;tt&gt;config/environment.rb&lt;/tt&gt; above everything else I have:

&lt;pre&gt;&lt;code&gt;Process.setrlimit Process::RLIMIT_RSS, 1024*1024*150, Process::RLIM_INFINITY&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And to eliminate core dumps, in &lt;tt&gt;config/environments/production.rb&lt;/tt&gt; I have:

&lt;pre&gt;&lt;code&gt;Process.setrlimit Process::RLIMIT_CORE, 0, Process::RLIM_INFINITY&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Keeping the hard limit at infinity allows me to increase the limit at a later date, for example when running cron jobs.  To give cron jobs different limits I've added a &lt;tt&gt;config/environments/cron.rb&lt;/tt&gt; that slurps the production values then overrides as necessary:

&lt;pre&gt;&lt;code&gt;eval File.read("#{RAILS_ROOT}/config/environments/production.rb")

CachedModel.use_local_cache = false

# Cron jobs can use a much memory as they want.
Process.setrlimit Process::RLIMIT_RSS, Process::RLIM_INFINITY
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(That eval is in there because of that's the same hack that Rails::Initializer uses to expose &lt;code&gt;config&lt;/code&gt; in environment files, bleh.)
</description>
      <pubDate>Wed, 13 Sep 2006 00:44:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:f28e74e8-531c-43e5-816b-49fdab57d033</guid>
      <author>drbrain@segment7.net (Eric Hodel)</author>
      <link>http://blog.segment7.net/articles/2006/09/13/controlling-rails-process-size</link>
      <category>Rails</category>
      <category>Ruby</category>
      <trackback:ping>http://blog.segment7.net/articles/trackback/344</trackback:ping>
    </item>
    <item>
      <title>"Controlling Rails Process Size" by Daniel Berger</title>
      <description>&lt;p&gt;For people using Ruby 1.8.4 or earlier, you can use the proc-wait3 package (available on the RAA) to get the same functionality (and the same API).&lt;/p&gt;</description>
      <pubDate>Thu, 21 Sep 2006 13:37:14 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:ca2a5d1f-b9e8-457b-80f4-b06c1aa155e0</guid>
      <link>http://blog.segment7.net/articles/2006/09/13/controlling-rails-process-size#comment-349</link>
    </item>
  </channel>
</rss>
