AbstractBrain Answers About us →

How to reduce memory usage in Ruby

Question

Sidekiq workers consume a lot of memory: how can I fix that?

I have a Rails application that can usually run with about 1 GB of RAM. However there are some Sidekiq workers that constantly grow over time - for days - and consume a lot of memory (e.g. 2.5GB).

Are there any solutions?

Answer

The problem is related to Ruby threads and how memory allocation works at low-level. In many cases it’s not a memory leak, but just a memory bloat.

Here’s two effective solution that we have used in production to reduce the memory usage:

The environment variable is a no-brainer. Here’s the results that we got by adding it (max RAM in 16h):

Note that: