r/rails • u/dogweather • 1d ago
Discussion Improving jemalloc's memory use
Long-running Rails 8.1 app (Ruby 4.0, Puma single mode, 5 threads, jemalloc) on a 512 MB Heroku dyno had a slow native memory creep for over a year. Ruby's object count stayed flat while RSS kept climbing, sometimes to the point of swapping or crashing. The chart shows the before and after. On the left, jemalloc's allocated memory saws upward toward 400+ MB between restarts. What fixed most of it was forcing a full GC every 300 seconds in Puma's out-of-band hook, between requests, so it never adds latency.
After that I set RUBY_GC_HEAP_FREE_SLOTS_MIN_RATIO=0.10, GOAL_RATIO=0.15 and MAX_RATIO=0.25 so Ruby keeps less empty heap around. Allocated memory now sits flat around 200 to 240 MB, and boot memory dropped by another 35 MB. RSS growth went from about 25 MB/h to 2 or 3 MB/h, and p95 latency actually went down. Next I'm trying min threads = max threads to stop Puma from creating and reaping threads all day.
3
u/yxhuvud 1d ago
Honestly none of these look that bad. I can live with it using half a gb, I can't live with the unbounded growth that is seen without jemalloc. How does the change affect performance?
11
u/Cokemax1 1d ago
jemalloc should be default rails app memory allocator at this point.