Reducing Ruby memory footprint

Topics: Ruby, Ruby on Rails

Ruby processes can retain excess memory without a leak; fewer Sidekiq threads and a lower glibc arena limit can reduce that footprint.

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 memory bloat.

For Sidekiq, reduce the number of Sidekiq threads (e.g. use 10 threads instead of 25)

For Rails applications, set the environment variable MALLOC_ARENA_MAX=2 for tuning glibc memory behaviour.

Heroku sets that variable automatically, and this approach is also recommended by both the creator of Sidekiq and the maintainer of Puma.