Skip to content

Tag Archive for mod_rails

Phusion Passenger (mod_rails) Memory Management

I’ve been using Phusion Passenger (mod_rails) for about a month to run a production and staging machine with a Ruby on Rails app. So far things have been going really well. Installation was smooth and it’s been really peppy.

However, the app just got hit by a lot of people at once and stopped responding. I didn’t realize the default setting in Passenger is to spawn a max of 6 Rails instances simultaneously. The documentation recommends 2 instances for a VPS with 256MB of memory (almost exactly what I’m running). So, my server spawned the max and then ran out of memory which made it unresponsive.

I hit a couple of small walls while getting this working so I wanted to share the process.

On linux based machines you can run passenger-status to find out the number of Phusion Passenger application instances that are running.

----------- General information -----------
max      = 6
count    = 3
active   = 0
inactive = 3

----------- Applications -----------
/u/apps/myapp/releases/20080921194503:
  PID: 8792      Sessions: 0
  PID: 8789      Sessions: 0
  PID: 8784      Sessions: 0

You can see that the output show the default max value of 6. To change this number you need to edit your httpd.conf and set the PassengerMaxPoolSize option. Make sure this is outside of your VirtualHost or it won’t work… which, I obviously ran into. It should probably look something like this:

LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/local/bin/ruby
PassengerMaxPoolSize 2

Once you’re done with that restart Apache and Passenger. Now, to take a look at the memory that Apache and Passenger are using run passenger-memory-stats. You’ll see something like this:

------------- Apache processes -------------
PID   PPID  Threads  VMSize   Private  Name
--------------------------------------------
5842  1     1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
8757  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
8784  5842  1        10.4 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9137  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9141  5842  1        10.4 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9143  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9147  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9155  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9163  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9506  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9510  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9623  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9629  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9631  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9633  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9750  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9752  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9865  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
### Processes: 18
### Total private dirty RSS: 4.36 MB

-------- Passenger processes ---------
PID   Threads  VMSize   Private  Name
--------------------------------------
8749  20       6.0 MB   0.5 MB   /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/ApplicationPoolServerExecutable 0 /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/bin/passenger-spawn-server  /usr/local/bin/ruby  /tmp/passenger_status.5842.fifo
8758  1        5.5 MB   2.9 MB   Passenger spawn server
8789  1        61.6 MB  53.7 MB  Rails: /u/apps/myapp/releases/20080921194503
8792  1        61.8 MB  53.9 MB  Rails: /u/apps/myapp/releases/20080921194503
### Processes: 4
### Total private dirty RSS: 111.03 MB

Now, that may look like a lot of Apache instances running but you can see the “Total private dirty RSS” (real memory usage of the processes) is only 4.36 MB which is nothing in the scheme of things. More importantly, the real memory that the Rails instances are taking is 111.03 MB which my server can handle. Remember, you’ll probably have MySQL and other applications running along with these processes, so don’t up the PassengerMaxPoolSize to the point of bringing your server to its knees.