Ruby Performance
scaling your Ruby web app
Part 1: Garbage collection
MRI/REE Facts
• Mark & sweep GC
• freelists
• very expensive
• All refs are on the heap
• All code is on the heap
GC Settings
RUBY_HEAP_MIN_SLOTS 10k
RUBY_HEAP_SLOTS_GROWTH_FACTOR 1.8
Defaults
startup
+ 20k
+ 10k
+ 18k
...
10k
10k + 10k
10k + 10k + 18k
10k + 10k + 18k + 32.4k
...
RUBY_HEAP_SLOTS_INCREMENT 10k
10 000
20 000
38 000
70 400
128 720
A better strategy
• Stabilize on 1-2 heaps
RUBY_HEAP_MIN_SLOTS (480 000)
• Linear increment to keep it tight
RUBY_HEAP_SLOTS_GROWTH_FACTOR 1
RUBY_HEAP_SLOTS_INCREMENT (20 000)
Some tools
memprof, gdb, scrap
[68.63 MB] GET /stats/scrap
Number of objects : 815266 (627150 AST nodes, 76.93%)
Heap slot size : 20
GC cycles so far : 352
Number of heaps : 7
Total size of objects: 15923.16 KB
Total size of heaps : 18036.55 KB (2113.39 KB = 11.72%
unused)
Leading free slots : 18545 (362.21 KB = 2.01%)
Trailing free slots : 0 (0.00 KB = 0.00%)
Number of contiguous groups of 16 slots: 2685 (4.65%)
Number of terminal objects: 4697 (0.51%)
Thanks
Gleb Mazovetskiy. github.com/glebm

Ruby Performance. Part 1 (Garbage Collection)

  • 1.
    Ruby Performance scaling yourRuby web app Part 1: Garbage collection
  • 2.
    MRI/REE Facts • Mark& sweep GC • freelists • very expensive • All refs are on the heap • All code is on the heap
  • 3.
    GC Settings RUBY_HEAP_MIN_SLOTS 10k RUBY_HEAP_SLOTS_GROWTH_FACTOR1.8 Defaults startup + 20k + 10k + 18k ... 10k 10k + 10k 10k + 10k + 18k 10k + 10k + 18k + 32.4k ... RUBY_HEAP_SLOTS_INCREMENT 10k 10 000 20 000 38 000 70 400 128 720
  • 4.
    A better strategy •Stabilize on 1-2 heaps RUBY_HEAP_MIN_SLOTS (480 000) • Linear increment to keep it tight RUBY_HEAP_SLOTS_GROWTH_FACTOR 1 RUBY_HEAP_SLOTS_INCREMENT (20 000)
  • 5.
    Some tools memprof, gdb,scrap [68.63 MB] GET /stats/scrap Number of objects : 815266 (627150 AST nodes, 76.93%) Heap slot size : 20 GC cycles so far : 352 Number of heaps : 7 Total size of objects: 15923.16 KB Total size of heaps : 18036.55 KB (2113.39 KB = 11.72% unused) Leading free slots : 18545 (362.21 KB = 2.01%) Trailing free slots : 0 (0.00 KB = 0.00%) Number of contiguous groups of 16 slots: 2685 (4.65%) Number of terminal objects: 4697 (0.51%)
  • 6.