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

Similar to Rails performance: Ruby GC tweaking

Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!
Dimelo R&D Team
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
Fabio Akita
 
Ruby off Rails (english)
Ruby off Rails (english)Ruby off Rails (english)
Ruby off Rails (english)
Stoyan Zhekov
 
At&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of RubyAt&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of Ruby
Coby Randquist
 
The secret of programming language development and future
The secret of programming  language development and futureThe secret of programming  language development and future
The secret of programming language development and future
Hiroshi SHIBATA
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
Amol Pujari
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBoss
JBug Italy
 
Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark fasterFive cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
Tim Ellison
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
victor dong
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
jackdong
 
XT Best Practices
XT Best PracticesXT Best Practices
XT Best Practices
Jeff Larkin
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summarydaniel.mattes
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyKyle Drake
 
Apache Sling - The whys and the hows
Apache Sling - The whys and the howsApache Sling - The whys and the hows
Apache Sling - The whys and the hows
Robert Munteanu
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
bobmcwhirter
 
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon Web Services
 
Taras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLTaras Matsyk - Event Driven ML
Taras Matsyk - Event Driven ML
PyCon Odessa
 

Similar to Rails performance: Ruby GC tweaking (20)

Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
 
Ruby off Rails (english)
Ruby off Rails (english)Ruby off Rails (english)
Ruby off Rails (english)
 
At&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of RubyAt&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of Ruby
 
Deployment de Rails
Deployment de RailsDeployment de Rails
Deployment de Rails
 
The secret of programming language development and future
The secret of programming  language development and futureThe secret of programming  language development and future
The secret of programming language development and future
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBoss
 
Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark fasterFive cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
 
XT Best Practices
XT Best PracticesXT Best Practices
XT Best Practices
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
PHP at Yahoo!
PHP at Yahoo!PHP at Yahoo!
PHP at Yahoo!
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
 
Apache Sling - The whys and the hows
Apache Sling - The whys and the howsApache Sling - The whys and the hows
Apache Sling - The whys and the hows
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
 
Concurrency in ruby
Concurrency in rubyConcurrency in ruby
Concurrency in ruby
 
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
 
Taras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLTaras Matsyk - Event Driven ML
Taras Matsyk - Event Driven ML
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 

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