问题与解决方案:rails中的gem也会影响到内存占用(more gems consume more memories in rails app)
访问量: 3615
在一个rails 应用中,都会遇到优化的问题。 ( 跟java不太一样。呵呵) ( every Rails app will meet the performance optimization problems when or before it is being delivered. -- which is not the same with JAVA, as far as I know, it is much less things to do for java app. )
而我最近遇到了一个问题,就是发现,不同的rails app占用的内存不一样。似乎这个问题主要由rubygems引起的。(recently I met a problem that different rails applications consume different memories, and it seems that the root cause of this problem is the rubygems )
项目A:引用了 38个gem, 运行起来占用 100MB 内存。 (app A: required 38 gems, and cost me 100 MB memories)
项目B: 引用了 10个 gem, 运行起来占用 30 MB 内存。(app B: required 10 gems and cost me 30 MB memories when running)
所以,优化gems的几个方法: ( so this is the solutions: )
1. 使用 groups. 这样在发布的时候,就可以忽略devlopment, test 这些group中定义的gem . 例如: ( define groups for different gems by purpose. e.g. 'production' environment will ignore those gems in 'development' groups)
# gems used only for test/dev purpose group :test, :development do gem "rspec-rails", ">= 2.5.0" gem 'factory_girl', '2.6.4' gem 'factory_girl_rails' end # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' end
2. 使用 require => false 来声明gem, 但是等到需要的时候才require . 例如: ( use :require => false to install the gems but not 'require' them automatically )
gem 'whenever', :require => false
参考资料:http://stackoverflow.com/questions/4800721/bundler-what-is-the-require-false-on-the-gemfile-means