mysql - 是什么决定了 Ubuntu 上使用 Rails 和 MySQL 的内存使用

标签 mysql ruby-on-rails memory memory-management

请原谅新手类型的问题,但是,是什么决定了 rails 和 MySQL(我的服务器是 Ubuntu)消耗的 RAM?几乎没有任何请求进入服务器似乎徘徊在 2GB 的 1.5 左右。 MySQL 中存储了大约 100MB 的数据。该站点有大约 3500 名注册用户,当流量很高时,内存往往会达到 1.8 GB 左右的峰值。当流量较低或不存在时,它不会下降太多。

在 RoR 部署方面,RAM 消耗的主要因素是什么?我会假设数据库大小,但我的数据库大小远不及我的 RAM 消耗(但也许这是错误的思考方式?)。

谁能给我指点这方面的好资源,或者在这里向我解释一下?

谢谢。

最佳答案

我正在分析 Ubuntu 服务器中运行我的 Rails 3.2.6 应用程序的最精简配置是什么,使用 Nginx + Unicorn 配置时内存占用最少。并使用本地 postgres 数据库。

在 ubuntu 中删除了 whoopsie 和 apparmor 等许多服务后,只让最基本的过程我可以在两侧实例化我的 worker,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 模型,而不是更多。

关于mysql - 是什么决定了 Ubuntu 上使用 Rails 和 MySQL 的内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1512365/

相关文章:

ruby-on-rails - virtualbox + vagrant 和 VMWare 融合的区别

计算C中结构分配的内存空间

mysql - 从子查询 SQL 选择最大数据,但它显示子查询的所有结果

ruby-on-rails - 从 Rails 中的字符串列中删除所有字母

mysql - MySQL中键、主键、唯一键和索引的区别

mysql - Ruby on Rails UTF8 问题

c++ - 派生类型的成员

java - 错误: not enough space for card marking array

php - MySQL 正则表达式 LIKE

MySQL查询优化: how can I speed up this query?