Skip to content

Instantly share code, notes, and snippets.



Forked from jjb/
Created Jan 16, 2017
What would you like to do?
Trying to figure out performance impact of RUBY_GC_HEAP_GROWTH_FACTOR


There are many options available for tuning ruby memory management:

The one that gets the most attention is RUBY_GC_HEAP_GROWTH_FACTOR, which is the only one Heroku suggests adjusting:

I've often wondered what the drawback was for setting RUBY_GC_HEAP_GROWTH_FACTOR quite low for a webapp. Wouldn't this only marginally affect performance while the app was initially booting up, and then therafter provide the most optimal memory size? So I set out to benchmark the behavior of different settings.

Assumption: since I'm benchmarking the time the memory manager takes to stop execution and allocate more memory, the code used for the benchmark can be anything which increases its memory usage as it runs. So diversity or realism is not needed.

benchmark code

  • ruby 2.3.1p112
  • macOS 10.11.6
  • MacBook Pro (Retina, 15-inch, Mid 2014)
  • 2.8 GHz Intel Core i7

s = []
start =
25_000_000.times do |i|
  print "[#{i/1_000_000}]" if 0 == i % 1_000_000
  s << "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"  
finish =

puts 'GC.stat[:heap_sorted_length]: ' + GC.stat[:heap_sorted_length].to_s
puts "seconds elapsed: " + (finish-start).to_s

sleep 100000000


➔ export RUBY_GC_HEAP_GROWTH_FACTOR=  #default. i don't know what that is
➔ ruby code.rb                      

GC.stat[:heap_sorted_length]: 82445
seconds elapsed: 6.431182
# memory, real memory, private memory: 1.78, 0.7, 0.6
➔ ruby code.rb                          
GC.stat[:heap_sorted_length]: 61783
seconds elapsed: 51.569609
# memory, real memory, private memory: 1.80, 1.51, 1.37
➔ ruby code.rb                         
GC.stat[:heap_sorted_length]: 67507
seconds elapsed: 9.097858
# memory, real memory, private memory: 1.80, 1.80, 1.67
➔ ruby code.rb                         
GC.stat[:heap_sorted_length]: 92055
seconds elapsed: 6.168111
# memory, real memory, private memory: 1.81, 1.81. 1.68
➔ ruby code.rb                       
GC.stat[:heap_sorted_length]: 93538
seconds elapsed: 5.383548
# memory, real memory, private memory: 1.80, 1.80, 1.70

Conclusions and questions

  • number of slots allocated seems to correspond with what one would expect from increasing growth factor
  • other than in the 1.01 case, time taken to stop code and allocate more memory is not significant, especially in the case of a web app booting up
  • i don't know what the difference is between memory, real memory, and private memory (as reported by Activity Monitor). when i was a kid, we just had resident memory and virtual memory, and WE WERE HAPPY
  • the default case is a wild outlier. If this were ruby 2.4 I would suspect it was using the RUBY_GC_HEAP_FREE_SLOTS_GOAL_RATIO algorithm ( So I haven't the slightest guess what might be going on here.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment