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…
I expected the RAM used to decrease, but instead, the swap memory decreased
Top comments (3)
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."
For any others that may come along, in Ruby 2.6.x it is:
For more information: bugs.ruby-lang.org/issues/15470
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!