Debugging and Profiling
      Rails App


David Paluy
January 2013
Ruby is eating RAM
Agenda

●   “Winter is coming!”
●   Garbage Collector
●   Debug Tools
●   Profiling Tools
The Task: Send ~30,000 e-mails
Result Before
How Ruby Works?

                       Physical RAM


                           Process Heap



     Ruby Heap                                        Ruby Heap


Ruby     Ruby     Ruby     Ruby     Ruby     Ruby
Object   Object   Object   Object   Object   Object
New Object allocation

Free List

    A
    L
    L
        F
    O
        R
    C
        E
    A
        E
    T
    E
    D
New Object allocation

Free List

    A
    L
    L
    O
    C
    A
    T
    E
    D
New Object allocation

Free List is empty

    A
    L
    L
    O
    C
    A
    T
    E
    D
New Object allocation

Free List is empty – Call GC

    A
    L
    L
    O
    C
    A
    T
    E
    D
GC Process

●   GC finds non-reachable objects and adds
    them to Free List
●   If Free List is still empty, another Heap
    allocated
MRI GC

●   “Conservative”: any bit pattern could be a
    pointer (may produce false positive)
●   “Stop the world”: no other Ruby code can
    execute during GC
●   “Mark & Sweep”:
    mark all objects in use, than sweep away
    unmarked objects
More Objects => Longer GC => Slow
In our case – Out of Memory!
How to Debug?

●   gem "pry-debugger"
    https://github.com/nixme/pry-debugger
●   gem "debugger-pry"
    https://github.com/pry/debugger-pry
Tools

●   ObjectSpace.count_objects
●   GC debug - Enable heap dump support
●   gdb.rb (only Linux)


    Note: memprof works only with Ruby 1.8
ObjectSpace.count_objects
Enable heap dump support to Ruby

Install custom patched version of ruby



Usage:
https://github.com/tmm1/gdb.rb

     Attached to existing process and
           examine the HEAP
Result After
Profiling Tools

●   Ruby Benchmark
●   ruby-prof
●   perftools.rb (Google perftools for Ruby)
Benchmark

●   gem 'benchmark_suite'
    https://github.com/evanphx/benchmark_suite
ruby-prof

gem 'ruby-prof'
https://github.com/rdp/ruby-prof
ruby-prof Measurements

●   process time (RubyProf::PROCESS_TIME)
●   wall time (RubyProf::WALL_TIME)
●   cpu time (RubyProf::CPU_TIME)
●   object allocations (RubyProf::ALLOCATIONS)
●   memory usage (RubyProf::MEMORY)
●   garbage collections runs (RubyProf::GC_RUNS)
●   garbage collection time (RubyProf::GC_TIME)
perftools.rb
https://github.com/tmm1/perftools.rb

gem 'rack-perftools_profiler', :require => 'rack/perftools_profiler'
rack-perftools_profiler usage
KCacheGrind
Summary

●   More Objects => Longer GC => Slow
●   Examine your HEAP
●   Use Tools
Q&A

http://dpaluy.github.com

@dpaluy


dpaluy@gmail.com


http://www.linkedin.com/in/davidpaluy

Debugging and Profiling Rails Application