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

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
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
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
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...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

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