As of the July 2019 EY stack v5 release, stable-v5-3.0.61, it is now possible to run Ruby with jemalloc enabled. In many cases this can noticeably reduce the memory consumption of your Rails application.
Ruby, by default, uses the default memory allocation algorithm in libc, called malloc. Make no mistake, this is a very good allocator and a sensible default algorithm, and it will very likely remain the default for Ruby.
However, malloc tends to suffer from increased memory fragmentation over time, which results in high memory consumption for Ruby processes. This is especially pronounced for background worker processes like Sidekiq.
How to Enable on EY Cloud
Right now Ruby with jemalloc is available on EY stack stable-v5 for Ruby 2.4, 2.5, and 2.6. It can be enabled by setting the environment variable EY_RUBY_JEMALLOC to "true".
Here is how to do that step-by-step:
Navigate to the environment you want to enable Ruby with jemalloc for and ensure the stack version is at least stable-v5-3.0.61, if not, Upgrade the environment. Also make sure that the Ruby version is 2.4 or above, if not you will need to Edit your environment and change the Ruby version, after thorough application testing in a local deployment and EY staging environment.
Open the Environment Variables options page.
Add the EY_RUBY_JEMALLOC variable with the value "true".
Run an "Apply" on the environment.
- To check if the version sporting jemalloc installed successfully, run 'ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"' and check for '-ljemalloc' in the output.
Restart the Ruby processes. This is quickest done through a Deploy, as this will restart all application and background workers on all instances. If you are looking to test a particular worker, then issue a specific monit restart command on the relevant instance.
Attention: Enabling jemalloc for Ruby is a significant change and may have unintended consequences. Always test this on a testing environment before enabling it on production!
In the case of multiple applications deployed into a single environment: As the sole system Ruby installation is used by all applications on an instance, applying this setting to one application will apply to all applications sharing that environment.
If you have further questions regarding this feature, please open a support ticket. We will be glad to help you.