Thursday, June 29, 2023

How to reduce memory usage for your Rails app - R14 - Memory Quota Exceeded in Ruby (MRI)

If you have just started building your Rails app and discovered that your memory quota is reaching nearly 1 GB - that's not normal. Fortunately, I have the exact solution for you. I've successfully brought my Rails app - Receipt-AI's memory usage from 1 GB to 268 MB.


1. Lower your worker count

The default Puma worker pool size is 4, but you can reduce it if you are not using a lot of concurrent connections.

heroku config:set WEB_CONCURRENCY=1


2. Remove unused gems and set some to require: false

It could be that the gems you used are not required for boot time. To determine how much memory your gems consume during boot time:

group :development do
gem 'derailed'

and then run bundle install and

bundle exec derailed bundle:mem

Bear in mind that if any gems is set to require: false, make sure to add require to the file where it is used.


3. Use rack-mini-profiler to monitor your database queries.

Uncomment this gem in your Gemfile and run bundle install. This tool allows you to measure the speed of your HTML page. It's super straightforward.

# Add speed badges []
# gem "rack-mini-profiler"


4. Caching

Rails has a built-in way to cache your view, as simple as:

<%= render partial: 'hero', cached: true %>

You should only use caching on partials that are used frequently and whose output does not change frequently, such as your home page, which is also the most visited.


5. Garbage collector turning

I have read multiple sources that state the default value of RUBYGCHEAPGROWTHFACTOR in Ruby is 1.8. Some sources mention that it may vary depending on the Ruby version. Nevertheless, you can configure the value by running:

heroku config:set RUBY_GC_HEAP_GROWTH_FACTOR=1.03

With this approach, your Rails app will be running super crisp and smooth without the need to upgrade the dyno.




Ruby memory use


Garbage collection tuning


ruby on rails


Memory Quota Exceeded

receipt scanner

Manage your Receipts with AI and SMS.

Find your inner peace

Start texting receipts →

Receipt-AI with AI and SMS

What's new? How does Receipt-AI grow as a startup

2023 Receipt AI - Big Green Company Terms Privacy

Use Case

QuickBooks Receipt Scanner with Receipt-AI Your Top Choice Xero Receipt App Generate a Simple Expense Report with AI and SMS Receipt Management for Small Businesses Receipt-AI: The Best Receipt Organizer Track Receipts with PO numbers for Construction Projects


🔍 The new Search Engine for your Receipts ⭐️ Introducing the new Receipt Insight 2.0 Receipt-AI is Now Officially Available on the QuickBooks Store 🗂️ Folder AI 2.0 - Email First time vs Many many times Partnerships with QuickBooks Store From Humble Beginnings to Community Accomplishments Where can I find the receipts in QuickBooks or Xero? 7 reasons why your company should use Receipt-AI 🗂️ Introducing the new Folder AI As of today, Receipt-AI has reached a milestone of 100k in total expenses. Hollywood and Silicon Valley Text Receipts and PO Numbers for Construction Projects Two tigers cannot share the same mountain Why do we start Receipt-AI? QuickBooks Disconnected The best receipt scanner for QuickBooks Upload receipts to QuickBooks with SMS What is expense report? 📦 New Receipt Insights How to reduce memory usage for your Rails app - R14 - Memory Quota Exceeded in Ruby (MRI) From Free Trial to Paid Trial Receipt-AI new feature: Upload Receipts using Email How to switch hosting from Render to Heroku for your Ruby on Rails application Receipt-AI: Take a picture and text