A few months back schmalowsky (not sure of real name) posted to the Passenger issue tracker about a patch to limit Passenger memory usage. It's kind of a brutal patch in that it uses setrlimit, so if the process tries to allocate too much memory it just dies. But hey, it keeps one Passenger process from gobbling up all the memory on the box, and you don't need Monit to watch it, so, good enough.
That said, the patch is a few months old and the Passenger source code has moved around, so the patch no longer applies cleanly. I've brought it up to date and you can get the diff here. It seems to work fine - when I set it low enough the process gets put down as soon as it exceeds the threshold.
To try this patch out, do something like this:
git clone git://github.com/FooBarWidget/passenger.git cd passenger wget http://infoether.com/~tom/passenger_memlimit.diff git apply passenger_memlimit.diff rake package:gem sudo gem install pkg/passenger-2.2.2.gem --no-rdoc --no-ri sudo /usr/local/bin/passenger-install-apache2-module --auto # now edit your httpd.conf and add "PassengerMemLimit 192M" /sbin/service httpd configtest sudo /sbin/service httpd restart
I fiddled with this patch for quite a while on my Macbook - but setrlimit(RLIMIT_RSS, nnn) doesn't seem to be taking effect. The function call is returning 0, and a subsequent getrlimit returns the value that I just set, but the process size grows and grows regardless. Not sure what's up with that. This is on OS X 10.5.7, Darwin Kernel Version 9.7.0. If anyone has insights into that please send them my way, thanks! Update: Eric Hodel tweets "setrlimit wasn't fully implemented on OS X, RSS and others are missing." Thanks Eric!
Also, mad props to schmalowsky for writing the original code. All I did was juggle it around a bit; the real work was already done. And of course a huge thanks as always to the Phusion guys for Passenger!
setrlimit wasn't fully implemented in OS X, so you can't limit RSS and some other things. I wanted to use this on OS X for limiting Rails' process growth back in the early days and heard this from some other OS X guy.
Posted by: drbrain | June 04, 2009 at 05:26 PM
@drbrain, cool, thanks for the info! Yeah, my googling brought me across your Process::setrlimit post from 2006... it's a bummer.
Posted by: tomcopeland | June 04, 2009 at 05:32 PM
Tom, if you haven't seen my recent post about memory usage in Ruby daemons, give it a look. It's solved our mysteriously large daemon problem. Your solution sounds like a good backup in case of memory leaks, etc.
http://www.mikeperham.com/2009/05/25/memory-hungry-ruby-daemons/
Posted by: Mike Perham | June 04, 2009 at 06:28 PM
@mike, that's great info. I'm also using REE and will be rebuilding it with those settings. Thanks for the pointer!
Posted by: tomcopeland | June 04, 2009 at 07:28 PM
I've been using a scheduled ruby script to do this sort of thing. The nice thing about it is that it give the process time to finish up the current request before out right killing it. It sounds like this memlimit patch does not.
Anyway, you can find the script here: http://gist.github.com/41713
Posted by: Wesley Moxam | June 05, 2009 at 11:19 AM
@wesley, yeah, this patch causes the process to quit as soon as it exceeds the memory usage. So it's pretty abrupt. But on the other hand, it'll make sure things stay under control... it's a tradeoff, definitely.
Posted by: tomcopeland | June 05, 2009 at 12:44 PM