什么决定了在Ubuntu上使用Rails和MySQL时的内存使用情况

3
请原谅我这个初学者的问题,什么决定了Rails和MySQL消耗的RAM(我的服务器是Ubuntu)?即使没有请求,服务器似乎仍在1.5到2GB之间波动。MySQL中存储着大约100MB的数据。网站有大约3500名注册用户,当流量较高时,内存往往会达到1.8GB左右。当流量较低或没有流量时,它并没有下降太多。
在RoR部署时,内存消耗的重要因素是什么?我本来以为是数据库大小,但我的数据库大小远远没有达到内存的消耗(也许这种想法是错误的?)。
有人能指点一下我吗?或者在这里向我解释一下吗?
谢谢。
2个回答

2
我正在研究在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模型,而不是更多。

1

EngineYard发布了一篇好的博客文章,其中讨论了Rails中可能导致内存问题的一些潜在来源。你是如何提供网站服务的?(Passenger? Mongrel?)


乘客。我有一个2GB的切片(slicehost)。 - chrishomer
还有几个问题:你使用的是哪个Ruby版本?你的PassengerMaxPoolSize是多少?是用Nginx还是Apache?我猜在我提供的博客文章中可能有一些可以帮到你的信息,但额外的信息也不会有坏处。 - Greg Campbell
Ruby 1.8.7 Apache PassengerMaxPoolSize --> 我没有设置它(所以是6?),但现在我查了一下,也许我应该设置一下? - chrishomer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接