4. The Great Migration
Old
● 240 Ruby Processes
● 20 Servers
● 12 Processes per Server
● 1 Thread per Process
5. The Great Migration
Old
● 240 Ruby Processes
● 20 Servers
● 12 Processes per Server
● 1 Thread per Process
New
● 400 Ruby Threads
● 10 Servers
● 4 Processes per Server
● 10 Threads per Processes
42. Option 2: Use a Different Memory Allocator
Can switch to jemalloc* over standard glibc malloc with
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
* After installing it via homebrew/apt-get/yum/etc
44. So… What should you do?
Default Reduced Arena Count jemalloc
Pros Out of the Box Works with all ruby
Significant memory
reduction
Best Memory Usage
Cons Heavy Fragmentation Increases contention in
ruby implementations
with true threading
(JRuby)
Requires reinstalling
ruby and installing
jemalloc
Use Cases Single-threaded Ruby
Programs on OS’s you
don’t control
Can set environment
variables but cannot
install packages
Anytime you’re able to
45. So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
46. So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
47. So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
“On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall
speedup (not just memory, end-to-end speedup) of around 11%”
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
48. So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
“On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall
speedup (not just memory, end-to-end speedup) of around 11%”
“I'm another Ruby user that used to have memory bloat problems and
switched to jemalloc as well”
49. Want to Learn More?
Go to Aaron Patterson’s Talk:
“Compacting Heaps in Ruby 2.7”