This post originated from an RSS feed registered with Ruby Buzz
by Eric Hodel.
Original Post: Controlling Rails Process Size
Feed Title: Segment7
Feed URL: http://blog.segment7.net/articles.rss
Feed Description: Posts about and around Ruby, MetaRuby, ruby2c, ZenTest and work at The Robot Co-op.
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).
Now, in Ruby 1.8.5, we have Process::setrlimit:
$ ri Process::setrlimit
----------------------------------------------------- Process::setrlimit
Process.setrlimit(resource, cur_limit, max_limit) => nil
Process.setrlimit(resource, cur_limit) => 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)
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.
At the top of config/environment.rb above everything else I have:
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 config/environments/cron.rb that slurps the production values then overrides as necessary:
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
(That eval is in there because of that's the same hack that Rails::Initializer uses to expose config in environment files, bleh.)