SlideShare a Scribd company logo
Rails performance:
                      Ruby GC tweaking


     #ParisRB                        Vlad ZLOTEANU
     August 2, 2001                           @vladzloteanu
                                 Software engineer @ Dimelo

Copyright Dimelo SA                                  www.dimelo.com
What we develop


               Frontend products
               -Collaborative platforms, ‘forum-like’, white-labeled, for
               big accounts
               (a kind of GetSatisfaction / UserVoice, but.. Better ;) )



               Backend product
               -SocialAPIs client
               -(a kind of TweetDeck, but for multiple channels, and
               designed for teams/multiple users per account)



Copyright Dimelo SA                                               www.dimelo.com
Stats on Dynamic requests


                      - on HTML/XHTML dynamic requests (we don’t count CSS, JS, images, etc)



                                                           20 req/s
                                                                            Web
                                                 Peaks of 40 req/s



                                                         10 req/s            APIs
                                                 Peaks of 25 req/s




Copyright Dimelo SA                                                             www.dimelo.com
Problem




                Medium response time ~ 400 ms

                                                WTF??




Copyright Dimelo SA                                     www.dimelo.com
Ruby perf toolbox


    Benchmark
                       Server logs Apache Benchmark (ab, ab2)

    Profile            Rack::Bug RailsBench RubyProf RBTrace
                       Memprof Scrap BleakHouse .. Valgrind
                       ltrace

    Std surveillance
                       Cacti Nagios

   Surveilance & profiling
                   NewRelic RPM UnionStation


Copyright Dimelo SA                                        www.dimelo.com
Finding the problem(s)


                 - DB queries (unoptimized queries, n+1)

                 - External services (EG external RSS feeds)

                 - Lots of partials, low or inexistent cache

                 - Slow session storage

                 - Memory issues
                   - Unoptimized GC
                   - Memory leaks



Copyright Dimelo SA                                            www.dimelo.com
Memory / GC issues
                      - How do I know it’s a memory/GC issue?
                         - Server logs
                         - Profilers (memprof)
                         - Monitorization services (newrelic)




Copyright Dimelo SA                                             www.dimelo.com
Ruby Garbage Collector


                 - Conservative


                 - Stop-the-world


                 - Mark-and-sweep process




Copyright Dimelo SA                         www.dimelo.com
Ruby default GC settings

                 RUBY_HEAP_MIN_SLOTS=10000
                 -Initial number of heap slots. It also represents the
                 minimum number of slots, at all times
                 -a new rails app boots up with almost 500k objects
                 on the heap(mostly nodes)

                 RUBY_HEAP_SLOTS_INCREMENT=10000
                 -The number of new slots to allocate when all initial
                 slots are used

                 RUBY_HEAP_SLOTS_GROWTH_FACTOR=1.8
                 -Next time Ruby needs new heap slots it will use a
                 multiplicator
                 -New stab is almost double sized
Copyright Dimelo SA                                                www.dimelo.com
Ruby default GC settings (2)



                 RUBY_GC_MALLOC_LIMIT=8000000
                 -force GC after 8 MB malloc/realloc
                 -High traffic Rails servers can easily allocate more
                 than 8 MB per request

                 RUBY_HEAP_FREE_MIN=4096
                 -The number of free slots that should be present
                 after GC finishes running. If there are fewer slots
                 than those defined it will allocate new ones




Copyright Dimelo SA                                                 www.dimelo.com
Find your numbers



                 - Start with enough memory to hold Rails (Ruby’s
                   default is practically nothing)

                 - Increase it linearly if you need more (Ruby’s
                   default is exponential increase)

                 - Only garbage-collect every XX million malloc
                   calls (Ruby’s default is way too small)

                 - Benchmark, benchmark, benchmark..



Copyright Dimelo SA                                                www.dimelo.com
GitHub numbers


      • RUBY_HEAP_MIN_SLOTS=800000 (10000)
      • RUBY_HEAP_FREE_MIN=100000 (4096)
      • RUBY_HEAP_SLOTS_INCREMENT=300000(100
        00)
      • RUBY_HEAP_SLOTS_GROWTH_FACTOR=1(1.8)
      • RUBY_GC_MALLOC_LIMIT=79000000(8000000
        )


Copyright Dimelo SA                    www.dimelo.com
Demo (using ApacheBench)

           - Frontend app, 15 requests, concurrency 1

           Before optimisations:

           Memory usage: 130 MB

           Execution time(ms):     228 344 322.0 316 4797


           After optimisations:

           Memory usage: 160 MB
           Execution time(ms):     120 181 190.0 150 2256




Copyright Dimelo SA                                         www.dimelo.com
Conclusions



                 - use REE on prod (or Ruby 1.9)

                 - measure, measure, measure (don’t forget to
                   GC.enable_stats)

                 - optimise what matters

                 - And yes, 5 lines of code can make a difference ;)




Copyright Dimelo SA                                              www.dimelo.com
Thank you!


      • Questions?




Copyright Dimelo SA           www.dimelo.com

More Related Content

PDF
Opal,The Journey from Javascript to Ruby at Ruby Conf Kenya 2017 by Bozhidar ...
PDF
6年前に作ったプログラムにテストコードを書きました ^^); 〜〜 テスト駆動開発の薦め
ZIP
Rails and Merb
PDF
Opal chapter 4_a_new_hope
KEY
Opal - Ruby Style!! Ruby in the browser
PDF
Profiling Ruby
PPT
Ruby & Rails Error Handling
PDF
21 ruby exceptions
Opal,The Journey from Javascript to Ruby at Ruby Conf Kenya 2017 by Bozhidar ...
6年前に作ったプログラムにテストコードを書きました ^^); 〜〜 テスト駆動開発の薦め
Rails and Merb
Opal chapter 4_a_new_hope
Opal - Ruby Style!! Ruby in the browser
Profiling Ruby
Ruby & Rails Error Handling
21 ruby exceptions

Similar to Rails performance: Ruby GC tweaking (20)

PPT
Enhance you APDEX.. naturally!
PDF
Fisl - Deployment
PDF
Ruby off Rails (english)
PDF
At&T Interactive: The Many Facets Of Ruby
PDF
Deployment de Rails
PDF
The secret of programming language development and future
PPT
Ruby on rails
PDF
Camel and JBoss
PDF
Five cool ways the JVM can run Apache Spark faster
PDF
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
PDF
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
PDF
XT Best Practices
PDF
Railswaycon 2009 - Summary
PDF
PHP at Yahoo!
KEY
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
PDF
Apache Sling - The whys and the hows
PDF
TorqueBox at DC:JBUG - November 2011
KEY
Concurrency in ruby
PDF
Taras Matsyk - Event Driven ML
PDF
Build Programming Language Runtime with LLVM
Enhance you APDEX.. naturally!
Fisl - Deployment
Ruby off Rails (english)
At&T Interactive: The Many Facets Of Ruby
Deployment de Rails
The secret of programming language development and future
Ruby on rails
Camel and JBoss
Five cool ways the JVM can run Apache Spark faster
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
XT Best Practices
Railswaycon 2009 - Summary
PHP at Yahoo!
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Apache Sling - The whys and the hows
TorqueBox at DC:JBUG - November 2011
Concurrency in ruby
Taras Matsyk - Event Driven ML
Build Programming Language Runtime with LLVM
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Machine learning based COVID-19 study performance prediction
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Ad

Rails performance: Ruby GC tweaking

  • 1. Rails performance: Ruby GC tweaking #ParisRB Vlad ZLOTEANU August 2, 2001 @vladzloteanu Software engineer @ Dimelo Copyright Dimelo SA www.dimelo.com
  • 2. What we develop Frontend products -Collaborative platforms, ‘forum-like’, white-labeled, for big accounts (a kind of GetSatisfaction / UserVoice, but.. Better ;) ) Backend product -SocialAPIs client -(a kind of TweetDeck, but for multiple channels, and designed for teams/multiple users per account) Copyright Dimelo SA www.dimelo.com
  • 3. Stats on Dynamic requests - on HTML/XHTML dynamic requests (we don’t count CSS, JS, images, etc) 20 req/s Web Peaks of 40 req/s 10 req/s APIs Peaks of 25 req/s Copyright Dimelo SA www.dimelo.com
  • 4. Problem Medium response time ~ 400 ms WTF?? Copyright Dimelo SA www.dimelo.com
  • 5. Ruby perf toolbox Benchmark Server logs Apache Benchmark (ab, ab2) Profile Rack::Bug RailsBench RubyProf RBTrace Memprof Scrap BleakHouse .. Valgrind ltrace Std surveillance Cacti Nagios Surveilance & profiling NewRelic RPM UnionStation Copyright Dimelo SA www.dimelo.com
  • 6. Finding the problem(s) - DB queries (unoptimized queries, n+1) - External services (EG external RSS feeds) - Lots of partials, low or inexistent cache - Slow session storage - Memory issues - Unoptimized GC - Memory leaks Copyright Dimelo SA www.dimelo.com
  • 7. Memory / GC issues - How do I know it’s a memory/GC issue? - Server logs - Profilers (memprof) - Monitorization services (newrelic) Copyright Dimelo SA www.dimelo.com
  • 8. Ruby Garbage Collector - Conservative - Stop-the-world - Mark-and-sweep process Copyright Dimelo SA www.dimelo.com
  • 9. Ruby default GC settings RUBY_HEAP_MIN_SLOTS=10000 -Initial number of heap slots. It also represents the minimum number of slots, at all times -a new rails app boots up with almost 500k objects on the heap(mostly nodes) RUBY_HEAP_SLOTS_INCREMENT=10000 -The number of new slots to allocate when all initial slots are used RUBY_HEAP_SLOTS_GROWTH_FACTOR=1.8 -Next time Ruby needs new heap slots it will use a multiplicator -New stab is almost double sized Copyright Dimelo SA www.dimelo.com
  • 10. Ruby default GC settings (2) RUBY_GC_MALLOC_LIMIT=8000000 -force GC after 8 MB malloc/realloc -High traffic Rails servers can easily allocate more than 8 MB per request RUBY_HEAP_FREE_MIN=4096 -The number of free slots that should be present after GC finishes running. If there are fewer slots than those defined it will allocate new ones Copyright Dimelo SA www.dimelo.com
  • 11. Find your numbers - Start with enough memory to hold Rails (Ruby’s default is practically nothing) - Increase it linearly if you need more (Ruby’s default is exponential increase) - Only garbage-collect every XX million malloc calls (Ruby’s default is way too small) - Benchmark, benchmark, benchmark.. Copyright Dimelo SA www.dimelo.com
  • 12. GitHub numbers • RUBY_HEAP_MIN_SLOTS=800000 (10000) • RUBY_HEAP_FREE_MIN=100000 (4096) • RUBY_HEAP_SLOTS_INCREMENT=300000(100 00) • RUBY_HEAP_SLOTS_GROWTH_FACTOR=1(1.8) • RUBY_GC_MALLOC_LIMIT=79000000(8000000 ) Copyright Dimelo SA www.dimelo.com
  • 13. Demo (using ApacheBench) - Frontend app, 15 requests, concurrency 1 Before optimisations: Memory usage: 130 MB Execution time(ms): 228 344 322.0 316 4797 After optimisations: Memory usage: 160 MB Execution time(ms): 120 181 190.0 150 2256 Copyright Dimelo SA www.dimelo.com
  • 14. Conclusions - use REE on prod (or Ruby 1.9) - measure, measure, measure (don’t forget to GC.enable_stats) - optimise what matters - And yes, 5 lines of code can make a difference ;) Copyright Dimelo SA www.dimelo.com
  • 15. Thank you! • Questions? Copyright Dimelo SA www.dimelo.com