DEV Community

loading...
Cover image for Ruby + jemalloc on a micro EC2 instance

Ruby + jemalloc on a micro EC2 instance

Gonzalo Moreno
・1 min read

After reading several articles about how badly it manages ruby memory, I wanted to do a test on a small application that I run with Ruby on Rails and Sidekiq to see if there are improvements in the performance of my application on a t2.micro instance (only 1Gb RAM).

So I got down to work and followed the simple steps to move from a Ruby that uses glibc by default to Ruby with jemalloc

First, update and install jemalloc (instructions for Debian/Ubuntu)

$ sudo apt update
$ sudo apt install libjemalloc-dev

Then, reinstall Ruby with jemalloc as option. On my server, I have Ruby 2.5.1 installed, so the command I executed was the following (works with RVM too):

$ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.5.1 

To ensure that your Ruby uses jemalloc, run the following command:

$ ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"

-lpthread -ljemalloc -ldl -lcrypt -lm 

And the results so far…

Some data from grafana

I expected the RAM used to decrease, but instead, the swap memory decreased

meme

More information

This awesome post

More detailed tutorial

Another tutorial

Discussion (3)

Collapse
pjmartorell profile image
Pere Joan Martorell

Read this: github.com/fullstaq-labs/fullstaq-...

"The Ruby core team have debated for years on whether to incorporate Jemalloc, and so far they've only been reluctant. Furthermore, Hongli Lai's research and discussions with various experts have revealed that the only way to make optimal use of Jemalloc is through the LD_PRELOAD mechanism: compiling Ruby with --with-jemalloc is not enough! LD_PRELOAD is such an intrusive and platform-specific change, that we're confident that the Ruby core team will never accept using such a mechanism by default."

Collapse
wevtimoteo profile image
Weverton Timoteo

For any others that may come along, in Ruby 2.6.x it is:

RbConfig::CONFIG['MAINLIBS']

For more information: bugs.ruby-lang.org/issues/15470

Collapse
rodolfobandeira profile image
Rodolfo

I've been using Jemalloc with Ruby 2.6.6. It just solved all my problems for Rails Sidekiq eating memory to the infinite. It is just amazing!