SlideShare a Scribd company logo
1 of 15
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 - DeploymentFabio 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 RubyCoby 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 futureHiroshi SHIBATA
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBossJBug 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 fasterTim 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 PracticesJeff 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 howsRobert Munteanu
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011bobmcwhirter
 
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 Floor28Amazon Web Services
 
Taras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLTaras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLPyCon 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

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

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