5

how to add jemalloc in a working ruby on rails server?We installed ruby using rvm.

Rails version:5.2 Ruby version:2.5.1

I tried

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

whose output i got as

-lpthread -lgmp -ldl -lcrypt -lm

I saw an article Lower Memory Usage of your Rails App with Jemalloc but its using rbenv

Jose Kj
  • 2,912
  • 2
  • 28
  • 40

2 Answers2

6

I managed to add jemalloc using the following steps:

Install the Jemalloc library, preferably using your distro's package manager. (apt, pacman, brew, etc.):

# For instance, on Ubuntu:
sudo apt install libjemalloc-dev

Reinstall the currently installed ruby version with a compilation flag to include Jemalloc support:

rvm reinstall 2.6.6 -C --with-jemalloc

Older versions of ruby used the compilation flag syntax -with-jemalloc (with a single dash) but Ruby 2.6 and up use --with-jemalloc (with a double dash).

Then check that Jemalloc support has been added properly:

# For ruby >= 2.6:
ruby -r rbconfig -e "puts RbConfig::CONFIG['MAINLIBS']"
# For ruby < 2.6:
ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"

It should output something like:

-lpthread -ljemalloc -lgmp -ldl -lcrypt -lm
Qqwy
  • 5,214
  • 5
  • 42
  • 83
ste26054
  • 419
  • 3
  • 11
  • 1
    It needs two dashes in the "rvm reinstall" command --with-jemalloc. Also beware - many comment platforms automatically replace two dashes with an mdash - which caused me an hour of confusion since it looks similar :) ALSO - the command to test has changed, you need to use MAINLIBS instead of LIBS – Kevin Mar 16 '21 at 20:19
5

Updates to @ste20654 answer

For me this command

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

returned

-lm

What worked is this

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

OR

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

which returned ( if ruby is correctly compiled with jemalloc )

-lz -lpthread -lrt -lrt -ljemalloc -lgmp -ldl -lcrypt -lm
techvineet
  • 5,041
  • 2
  • 30
  • 28
  • 1
    Valid as 2021 with ruby 2.7, the popular answer did not display the ljemalloc but this option did – damuz91 Aug 20 '21 at 02:19