Halve Your Memory Usage With These 12 Weird Tricks
summary
Claims twelve concrete tricks that can halve your memory usage, targets MRI/C Ruby, and gives numbers: aim for 300 megabytes per instance, expect logarithmic growth and long steady-state windows (12–24+ hours). Explains GC thresholds, object-space pages/408 slots, heap fragmentation, and shows tools that find culprits: Scout/Skylight, oink, memory_profiler, derailed. Presents operational fixes that work in practice: jemalloc (Heroku buildpack), copy-on-write preload with Puma/Unicorn, malloc_arena_max and Minitest/Hell for threading. Results are demonstrable, not just theory.
AI-written summary · about
chapters (26)
- 0:00Intro
- 2:02RUBY IS A GARBAGE COLLECTED LANGUAGE. BUT MY MEMORY IS GROWING. THEREFORE, MEMORY LEAK.
- 3:11DIAL BACK THE INSTANCE COUNTS DISCOVER TRUE STEADY-STATE MEMORY USAGE PER INSTANCE
- 5:08MYTH: MEMORY USAGE SHOULD LOOK LIKE A LONG, FLAT LINE
- 8:57AIM FOR 300MB PER INSTANCE THIS ALSO APPLIES TO SIDEKIQ
- 9:38STOP ALLOCATING SO MANY OBJECTS AT ONCE
- 9:50MYTH: "SHOULDN'T GC CLEAN THE UNUSED OBJECTS UP AFTER THE JOB COMPLETES?"
- 10:10THRESHOLDS HEAP FRAG FREE ISN'T FREE
- 10:54THRESHOLDS NOT TIMERS.
- 16:39MALLOC AND FREE ARE SUGGESTIONS, NOT COMMANDS
- 20:05HEAP FRAGMENTATION CAN CAUSE LONG-TERM SLOW "LEAKS"
- 23:44MAKE YOUR OWN: RubyConf OBJECTSPACE & GC.STAT
- 24:46SOLUTION 2C: IF ALL ELSE FAILS, MOVE TO RAKE TASKS THROWAWAY VMS ARE BETTER THAN BLOATED VMS
- 25:53GEMFILE AUDIT WITH derailed
- 26:31MYTH: DEPENDENCIES ARE FREE!
- 27:48EMPHASIZES FRAGMENTATION AVOIDANCE AND SCALABLE CONCURRENCY SUPPORT.
- 28:34OR --WITH-JEMALLOC
- 29:00USE COPY-ON-WRITE PUMA, UNICORN OR PASSENGER W/PRELOADING
- 29:22COPY-ON-WRITE Rubycon INCREASES SHARED MEMORY
- 32:12USE A THREADED WEBSERVER Punts, Passenger Enterprise. Increase concurrency w/lighter methods
- 32:35MINI-MYTH: MY APPLICATION ISN'T THREAD-SAFE
- 33:37KEEP RUBY AND GEMS UP-TO-DATE
- 33:52RUBY 2.2+ RAILS 4.2+ WATCH OUT FOR RUBY 2.4
- 34:45TUNE MALLOC
- 36:55mallopt
- 37:36GC TUNING CAN FIX: TOO MANY FREE SLOTS SLOW STARTUP TOO MANY OR TOO FEW GCS








