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.

5a2ea480fc0d39355b5bae9fe4ed6de6?s=128

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 So, why I am here talking about GC?

  3. 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…
  4. RailsConf 2016 So, let’s get it started and talk about

    Ruby Garbage Collector
  5. RailsConf 2016 Terms • GC: Garbage Collector • RGenGC: Restricted

    Generational GC • RIncGC: Restricted Incremental GC • CoW: Copy-on-write technique • ab: Apache Benchmark Tool
  6. 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
  7. RailsConf 2016 Why I am here… Ruby Garbage Collector

  8. RailsConf 2016 Why I am here… GC • Once upon

    a time there was a Rails App • Why? How? How can I?
  9. 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!!!! :-)
  10. RailsConf 2016 A bit about the history of Ruby GC

  11. 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/
  12. RailsConf 2016 A brief history about Ruby GC by Aman

    Gupta Reference: http://tmm1.net/ruby21-rgengc/
  13. RailsConf 2016 Ruby 1.8: Simple Mark & Sweep Reference: http://tmm1.net/ruby21-rgengc/

  14. RailsConf 2016 Ruby 1.9.3: Lazy Sweep Reference: http://tmm1.net/ruby21-rgengc/

  15. RailsConf 2016 Ruby 2.0: Bitmap Masking (CoW) Reference: http://tmm1.net/ruby21-rgengc/

  16. RailsConf 2016 Ruby 2.0.x: 2.0.0-p647

  17. RailsConf 2016 Ruby 2.1: RGenGC Reference: http://tmm1.net/ruby21-rgengc/

  18. 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/
  19. 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/
  20. RailsConf 2016 Ruby 2.1.x & 2.2.x

  21. RailsConf 2016 Configuration parameters

  22. 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
  23. 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
  24. 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
  25. 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
  26. RailsConf 2016 Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705 / https://github.com/ruby/ruby/blob/v2_2_3/gc.c#L6972 https://github.com/ruby/ruby/blob/v2_3_0/gc.c#L7256 Ruby v2.1.7 And

    also: v2.2.x & v2.3.x
  27. 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
  28. RailsConf 2016 My approach to measure & tuning

  29. RailsConf 2016 Me: measuring & tuning • Find out if

    your current/default experience is good, normal, bad, or really bad.
  30. 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…
  31. 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!
  32. 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!
  33. 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
  34. RailsConf 2016 And: either this or that • You may

    open your eyes to something else • Or you establish a laptop baseline DEV PROD
  35. RailsConf 2016 Finally: I got this

  36. 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
  37. 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…
  38. RailsConf 2016 Some of my findings

  39. 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
  40. 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)
  41. RailsConf 2016 Ruby 2.0.0 vs Ruby 2.1.7

  42. RailsConf 2016 v2.0.0 v2.1.7

  43. RailsConf 2016 v2.0.0 v2.1.7

  44. RailsConf 2016 Ruby 2.1.7 before vs after

  45. RailsConf 2016 v2.1.7 before v2.1.7 after

  46. RailsConf 2016 v2.1.7 before v2.1.7 after

  47. RailsConf 2016 Ruby 2.2.3 before vs after

  48. RailsConf 2016 v2.2.3 before v2.2.3 after

  49. RailsConf 2016 v2.2.3 before v2.2.3 after

  50. RailsConf 2016 Ruby 2.1.7 vs 2.2.3 (after)

  51. RailsConf 2016 v2.1.7 after v2.2.3 after

  52. RailsConf 2016 v2.1.7 after v2.2.3 after

  53. RailsConf 2016 Thank you! Q&A?! Helio Cola http://hac-rods.me @hacrods

  54. RailsConf 2016 Thanks Thanks to these folks that helped me

    along the way NoTwitterErik costasd@something.gr