How to add jemalloc to existing rails server using rvm?
Asked Answered
B

2

7

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

Bolick answered 9/9, 2019 at 8:39 Comment(0)
P
9

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
Parasite answered 13/9, 2019 at 9:14 Comment(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 LIBSSaltwater
F
7

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
Fizgig answered 3/9, 2020 at 15:46 Comment(2)
Valid as 2021 with ruby 2.7, the popular answer did not display the ljemalloc but this option didSusannesusceptibility
Thanks! I was convinced --with-jemalloc didn't do anything, but it was that the way to check it had changedSaltwater

© 2022 - 2024 — McMap. All rights reserved.