chrishomer
chrishomer

Reputation: 4920

What Determines Memory Usage on Ubuntu with Rails and MySQL

Forgive the newbie type question but, what determines the RAM consumed by rails and MySQL (my server is Ubuntu)? With barely any requests coming in the server seems to be hovering around 1.5 of 2GB. MySQL has about 100MB of data stored in it. The site has about 3500 registered users and when trafic is high, the memory tends to peak around 1.8 GB. When the traffic is low or non-existent, it doesn't drop much though.

What are the big factors in RAM consumption when it comes to RoR deployments? I would have assumed DB size but my DB size is nowhere near my RAM consumption (but maybe this is the wrong way to think about it?).

Can anyone point me to a good resource on this, or explain it to me here?

Thanks.

Upvotes: 3

Views: 951

Answers (2)

Canimus
Canimus

Reputation: 41

I am analyzing what is the slimmest configuration in an Ubuntu Server to run my Rails 3.2.6 application with the lowest memory footprint with the Nginx + Unicorn configuration. And using a local postgres database.

After removing many services like whoopsie and apparmor in ubuntu, and let only the very basic processes I can instantiate my workers in both sides, nginx and unicorn for a total of 500MB.

This is purely the vanilla launch of the app. with a single database connection. This is the result of the commands executed to baseline with the first user:

$ free -mt
             total       used       free     shared    buffers     cached
Mem:          3001        550       2450          0         16        178
-/+ buffers/cache:        355       2646
Swap:          952          0        952
Total:        3954        550       3403

$ ps -ef | grep nginx
root      1232     1  0 12:54 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data  1233  1232  0 12:54 ?        00:00:00 nginx: worker process
www-data  1234  1232  0 12:54 ?        00:00:00 nginx: worker process
www-data  1235  1232  0 12:54 ?        00:00:00 nginx: worker process
www-data  1236  1232  0 12:54 ?        00:00:00 nginx: worker process
herminio  5292  1078  0 13:24 pts/1    00:00:00 grep nginx

$ ps -ef | grep unicorn
herminio  4863     1  0 13:01 ?        00:00:00 unicorn_rails master -c unicorn.rb -D -E production                                                          
herminio  4866  4863  2 13:01 ?        00:00:34 unicorn_rails worker[0] -c unicorn.rb -D -E production                                                                                                         
herminio  5296  1078  0 13:24 pts/1    00:00:00 grep unicorn

$ ps -ef | grep postg
postgres   935     1  0 12:54 ?        00:00:00 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf
postgres   940   935  0 12:54 ?        00:00:00 postgres: writer process                                                                                                    
postgres   941   935  0 12:54 ?        00:00:00 postgres: wal writer process                                                                                                
postgres   942   935  0 12:54 ?        00:00:00 postgres: autovacuum launcher process                                                                                       
postgres   943   935  0 12:54 ?        00:00:00 postgres: stats collector process                                                                                           
postgres  5215   935  0 13:12 ?        00:00:00 postgres: user_db pto_db_prod 127.0.0.1(47118) idle                                                                       
herminio  5300  1078  0 13:24 pts/1    00:00:00 grep postg

With this information I can determine that my OS uses 92 processes to host my application with 1 connection, as more processes are spawn from Nginx and Unicorn the number of processes increase +1 and also the connections to the database.

Looking into the memory usage per process can also help to determine the amount of memory consumption of your application.

I am using an old laptop to baseline my app, and it only has 3GB of memory. In the future I am planning to release this app, in a distributed environment with low spec servers, therefore I want to know specifically the footprint of everything in my rails app.

Some of the things I learn along the way are:

bundle install --without development test # To make sure your app uses and loads Gems that are only used in the production environment and not more.

Make sure you only load the ActiveRecord models that your request needs and not more.

Upvotes: 2

Greg Campbell
Greg Campbell

Reputation: 15292

EngineYard had a good blog post that discusses some potential sources for memory issues in Rails. How are you serving your site? (Passenger? Mongrel?)

Upvotes: 1

Related Questions