Ruby on Rails: Reduce Memory Usage by 22%
ruby on rails
Table of Contents
Using Jemalloc
# Use jemalloc with configuration
RUN apt-get install libjemalloc2
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
# 1. Sets a 1-second decay time for dirty pages
# 2. Configures 2 memory arenas to balance performance and memory usage
# 3. Enables a background thread for maintenance tasks to enhance application performance
ENV MALLOC_CONF=dirty_decay_ms:1000,narenas:2,background_thread:true
Less Memory + Faster!
Memory bloating issue reduced by 22% as it plateaus at ~2.9GB instead of ~3.7GB.
The NodeFlair application should also feel faster now as we reduced the response time by 12% (p0.9) and 55% (p0.99).
Reference
- Optimizing your deployment by Fly.io
- Malloc Can Double Multi-threaded Ruby Program Memory Usage by Nate Berkopec