1

My AWS credits are expiring soon so I want to reduce the RAM use of my app to 512mb by stripping non-essential features and gems. So far, my remaining gems are:

gem 'rails', '~> 5.2.4'
gem 'sass-rails', '~> 5.0'
gem 'jbuilder', '~> 2.5'
gem 'rack-cors', require: 'rack/cors'
gem 'devise', github: 'plataformatec/devise'
gem 'turbolinks', '~> 5'
gem 'chart'
gem 'font-awesome-sass'
gem 'friendly_id'
gem 'geocoder'
gem 'pg'
gem 'pg_search'
gem 'simple_form'
gem 'puma'
gem 'jquery-rails'
gem 'bootstrap'
gem 'uglifier', '>= 1.3.0'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'pagy'
gem 'sucker_punch'

I downgraded from Rails 6+ to Rails 5.2.4, passenger to puma, delayed_jobs to sucker_punch, kaminari to pagy. I tried switching to bulma but it had problems on mobile.

I still want to reduce memory further.

The app has 200 tables. Will reducing the number of models and using .pluck on queries help reduce RAM use?

Is uglifier and sass-rails needed?

Will removing js dependencies such as mapbox and other css also reduce RAM use?

Jun Dalisay
  • 851
  • 1
  • 8
  • 18
  • You need to tell us what you have found out so fat about what takes up your memory most in your application. If you have not tried debugging, debugging is the first thing you need to do when you want to optimize. Removing everything is not very practical. check out this gem https://github.com/schneems/get_process_mem and try to figure out your actual memory usage first. – andylee Dec 15 '19 at 16:53

1 Answers1

2

Is uglifier and sass-rails needed?

If you don't use sass on your project you can remove sass-rails. But I see you have font-awesome-sass gem.

Uglifier is used only when compiling assets so your css and js files are smaller.

The app has 200 tables. Will reducing the number of models and using .pluck on queries help reduce RAM use?

Rails autoloads your code, but I doubt the number of models has a significant impact on the ram usage. On the other hand, when you read a record from the database, rails has to create an ActiveRecord object on memory, if you have a table with hundreds of records and you just load all of them at once (User.all.to_a or User.all.each) it will require a lot of ram but only on that specific moment. Usually pluck is a good way to use less ram since you only fetch an array of the values you want and not complete AR objects when you don't need them.

Will removing js dependencies such as mapbox and other css also reduce RAM use?

I don't think so, js dependencies are used during assets compilation.

A good trick to free some ram if you use multiple threads is to use jemalloc instead of the standard malloc for memory allocation https://www.youtube.com/watch?v=4_yxbh9Enoc

Another thing you can do is to only load the rails modules you actually use. On your config/application.rb file you'll see a line like this require 'rails/all' that loads all rails features https://github.com/rails/rails/blob/master/railties/lib/rails/all.rb

You can change that line to only import the features you want, like if you don't use action_cable or active_job you can just import the rest.

Another thing you can do is remove gems related to third party js and css like bootstrap, font-awesome, jquery and use yarn to handle dependencies of js and css. You may lose some view helpers provided by those gems though.

arieljuod
  • 13,456
  • 2
  • 19
  • 31
  • Thanks! I'll try `jemalloc`. I removed `test_unit` and did more plucking but kept my models as suggested. I also kept `sass-rails` because reconfiguring each js via yarn is more work. I also deleted unneeded logic so they won't be autoloaded into memory. – Jun Dalisay Dec 15 '19 at 17:09
  • For precompiling, I added swap space: https://stackoverflow.com/questions/22272339/rake-assetsprecompile-gets-killed-when-there-is-a-console-session-open-in-produ – Jun Dalisay Dec 16 '19 at 04:23