There's been a lot of discussion recently about running Ruby with jemalloc. Sounds like some good memory savings to be had basically for free. I ran into an issue initially when compiling Ruby with the
--with-jemalloc option. On Mac OSX Sierra, compiling Ruby 2.4.2 from source and using jemalloc 5.0.1, I got this linker error:
One fix is a small change to the autoconf script,
configure.in. Ruby 2.4.1's
configure.in script had this but 2.4.2 didn't:
This kind of makes sense. Per the docs,
AS_CASE is a macro that generates a
case statement and adding
-ljemalloc to a list of libraries seems like what's needed here. It results in this change to the generated
Makefile, which also seems reasonable:
And then you can verify jemalloc is linked with:
Another way to verify it is to run a script under dtruss and the process will attempt to read a jemalloc config file:
If jemalloc isn't linked in, that
readlink system call won't happen.
Side note - at first I attempted to verify that jemalloc was included using FFI. I figured I'd just call a function that was declared in
jemalloc.c. So I did:
But then I realized that doesn't make sense.
ffi_lib will load up the specified library regardless of whether it's linked into the Ruby process, so that would never fail.
Finally, if you're using Ruby 2.5.1, it's the same change except since this commit the build process uses
configure.ac rather than
configure.in. But just paste in that same snippet on line 4045, bam done.