Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tweaking Ruby GC parameters for fun, speed, and profit

Tweaking Ruby GC parameters for fun, speed, and profit

Whether you are building a Robot, controlling a Radar, or creating a Web App, the Ruby Garbage Collector (GC) can help you. The stats exposed by the Garbage Collector since Ruby v2.1 caught my attention and pushed me to dig deeper. Both Ruby 2.1 and 2.2 brought great performance improvements. From a practical point of view, we will discuss how to use the GC to enhance the performance of your software, from configuration parameters to different approaches on how you can change them yourself.

Helio Cola

May 04, 2016
Tweet

More Decks by Helio Cola

Other Decks in Programming

Transcript

  1. RailsConf 2016 Tweaking Ruby GC parameters for fun, speed, and

    profit I mean: for fun! Helio Cola http://hac-rods.me @hacrods
  2. RailsConf 2016 hac-rods.me Hi, I am Helio Cola! • ~15

    years developing SW • ~10 years doing C/C++ on Solaris • 5/6 years since I found happiness • ~4 years full time happiness • I go by @hacrods on the internet…
  3. RailsConf 2016 Terms • GC: Garbage Collector • RGenGC: Restricted

    Generational GC • RIncGC: Restricted Incremental GC • CoW: Copy-on-write technique • ab: Apache Benchmark Tool
  4. RailsConf 2016 Agenda • Why I am here talking about

    Ruby GC • A couple bits about its history • Configuration parameters • My approach to measure & tuning • Some of my findings • QA
  5. RailsConf 2016 Why I am here… GC • Once upon

    a time there was a Rails App • Why? How? How can I?
  6. RailsConf 2016 Why I am here… GC • I did

    do a lot research on it… • I had a really great time learning & doing all this • I wanted to share it • And my talk got accepted!!!! :-)
  7. RailsConf 2016 A brief history about Ruby GC • Ruby

    1.8: Simple Mark & Sweep • Ruby 1.9.3: Lazy Sweep • Ruby 2.0: Bitmap Masking (CoW friendly) • Ruby 2.1: RGenGC • Ruby 2.2: RIncGC and Symbol GC Reference: http://tmm1.net/ruby21-rgengc/
  8. RailsConf 2016 A brief history about Ruby GC by Aman

    Gupta Reference: http://tmm1.net/ruby21-rgengc/
  9. RailsConf 2016 Ruby 2.2: RIncGC / Symbol GC • Symbols

    now gets collected by GC! • RIncGC https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/
  10. RailsConf 2016 Ruby 2.2: RIncGC / Symbol GC • RIncGC

    • Not a silver bullet • Don’t affect throughput or response time Reference: https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/
  11. RailsConf 2016 GC Parameters: ruby 2.0.x • RUBY_GC_MALLOC_LIMIT • RUBY_HEAP_MIN_SLOTS

    • RUBY_FREE_MIN https://github.com/ruby/ruby/blob/v2_0_0_647/gc.c#L3296
  12. RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_HEAP_INIT_SLOTS •

    RUBY_GC_HEAP_FREE_SLOTS • RUBY_GC_HEAP_GROWTH_FACTOR • RUBY_GC_HEAP_GROWTH_MAX_SLOTS • RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705
  13. RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_MALLOC_LIMIT •

    RUBY_GC_MALLOC_LIMIT_MAX • RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705
  14. RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_OLDMALLOC_LIMIT •

    RUBY_GC_OLDMALLOC_LIMIT_MAX • RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705
  15. RailsConf 2016 Running it $ export RUBY_GC_HEAP_INIT_SLOTS=800000 $ export RUBY_GC_HEAP_FREE_SLOTS=32768

    $ export RUBY_GC_MALLOC_LIMIT=33554432 $ export RUBY_GC_MALLOC_LIMIT_MAX=50331648 $ rails s
  16. RailsConf 2016 Me: measuring & tuning • Find out if

    your current/default experience is good, normal, bad, or really bad.
  17. RailsConf 2016 Me: measuring & tuning • Research mode, reading

    mode, studying mode, read ruby source code, compile it… ✴ I have to upgrade the one with 2.0.x ✴ I don’t know yet if my app with 2.1.x is in good or bad shape • And after a lot of research…
  18. RailsConf 2016 So, what now?! • Let me start by

    running it all on my laptop … and ab it! … and… still doesn’t look like my production app!
  19. RailsConf 2016 So, and now what?! • A couple of

    days go by… … testing, measuring, analyzing data, thinking about what I am seeing… • Some well known reasons: • My laptop isn’t like my AWS servers • I don’t have the same amount of data • My ab calls aren’t reflecting very well the traffic from my real users … so… the app on my laptop won’t be like it is in prod servers!
  20. RailsConf 2016 So… • Sleep on it • Go spend

    a day doing something else • Grab a teammate and talk to him • Explain what you are doing to someone else (or at least try to explain! :-) • Plan your next steps
  21. RailsConf 2016 And: either this or that • You may

    open your eyes to something else • Or you establish a laptop baseline DEV PROD
  22. RailsConf 2016 Change, Measure & Analyze • Do that over

    & over & over again… • Analyze your data, document your numbers in a piece of paper or notepad • Make one single change (at a time) and re-run your tests
  23. RailsConf 2016 Again: either this or that • If you

    will find something that improves your app ̣ Document it nicely and share with your team ̣ Make a small talk out of it ̣ And you won’t sweat to make it into the Production environment! • Or that will happen…
  24. RailsConf 2016 Some of my findings Parameters I changed: •

    RUBY_GC_HEAP_INIT_SLOTS • RUBY_GC_HEAP_FREE_SLOTS • RUBY_GC_MALLOC_LIMIT • RUBY_GC_MALLOC_LIMIT_MAX
  25. RailsConf 2016 Some of my findings A few comparisons: •

    Ruby 2.0.0 vs Ruby 2.1.7 (before) • Ruby 2.1.7: before and after • Ruby 2.2.3: before and after • Ruby 2.1.7 vs Ruby 2.2.3 (after)