我正在研究在Ubuntu Server中运行我的Rails 3.2.6应用程序时,使用最小内存占用的Nginx+Unicorn配置的最小配置。并且使用本地postgres数据库。
在Ubuntu中删除了许多服务(如whoopsie和apparmor)后,只留下非常基本的进程,我可以在nginx和unicorn两个方面实例化我的工作程序,总共500MB。
这仅是应用程序的原始启动。具有单个数据库连接的结果通过执行以下命令进行基准测试:
$ 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
通过这些信息,我可以确定我的操作系统使用92个进程来托管我的应用程序,其中包括1个连接。当从Nginx和Unicorn生成更多的进程时,进程数量会增加+1,并且与数据库的连接也会增加。
查看每个进程的内存使用情况还可以帮助确定应用程序的内存消耗量。
我正在使用一台旧笔记本电脑来基准测试我的应用程序,它只有3GB的内存。将来,我计划在低规格服务器的分布式环境中发布此应用程序,因此我想特别了解我的Rails应用程序中所有内容的占用空间。
我在学习的过程中得到了一些经验:
bundle install --without development test # 以确保您的应用程序仅使用和加载在生产环境中使用的Gem,而不是更多。
确保您仅加载请求所需的ActiveRecord模型,而不是更多。