'How to add jemalloc to existing rails server using rvm?

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



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Qqwy