Slide 1

Slide 1 text

Curious case of Ruby’s memory

Slide 2

Slide 2 text

Garbage Collection

Slide 3

Slide 3 text

How it used to work

Slide 4

Slide 4 text

Mark and Sweep

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Generational GC

Slide 7

Slide 7 text

Most objects die Young

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

active_users[user.id] = user Old gen object New object

Slide 10

Slide 10 text

Bug!

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

memcpy(RARRAY_PTR(ary)[i], src, array_len(src)) Copy into ruby array Object in old gen

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Key take aways

Slide 17

Slide 17 text

Don’t use low level access for Ruby DS in C extensions, such RARRAY_PTR

Slide 18

Slide 18 text

If your C-ext holds ref. to a Ruby object you need Write Barriers for taking adv. of Gen. GC.

Slide 19

Slide 19 text

GC Tuning via ENV variables

Slide 20

Slide 20 text

Part 2

Slide 21

Slide 21 text

Memory Profiling Ruby apps

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Thanks to Ruby 2.1

Slide 24

Slide 24 text

RBKit

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

RBkit Team • Emil • Kashyap • Yuva • Hemant And

Slide 30

Slide 30 text

http://codemancers.com

Slide 31

Slide 31 text

C is what Linus Torvalds wrote.

Slide 32

Slide 32 text

About RBKit • A low overhead Ruby Profiler built for MRI. • Written almost completely in C.

Slide 33

Slide 33 text

RBKit • A Desktop application • A Ruby gem

Slide 34

Slide 34 text

RBKit • Gather and send all profile data via ZeroMQ to connected client (Desktop app) • Uses msgpack as serialization format.

Slide 35

Slide 35 text

Advantage RBKit • ZeroMQ manages separate IO threads for sending and receiving messages. • Msgpack serialisation is fast. • A profiler that can be used in production!

Slide 36

Slide 36 text

Using it ! require 'rbkit' ! Rbkit.start_profiling

Slide 37

Slide 37 text

RbkitApp

Slide 38

Slide 38 text

RbkitApp • Cross platform - Qt/C++ • Webkit/Javascript for rendering charts. • Sqlite

Slide 39

Slide 39 text

Why Qt/C++? • All heavy lifting is done client side. • Parsing and processing heap dump of millions of objects. • We can still render certain pages/data using plain HTML via Qt Webkit bridge.

Slide 40

Slide 40 text

Tools used • Valgrind • leaks (Try this command on OSX now!)

Slide 41

Slide 41 text

RBkit Status Report • Memory Profiling works (Demo) • CPU Profiling is in works. • Open source

Slide 42

Slide 42 text

RbkitApp Demo

Slide 43

Slide 43 text

GC tuning visualizations

Slide 44

Slide 44 text

RUBY_GC_HEAP_INIT_SLOTS

Slide 45

Slide 45 text

RUBY_GC_HEAP_GROWTH_FACTOR

Slide 46

Slide 46 text

Ruby Heap vs C heap • Rvalue size = 40 bytes • If Object can’t fit that size, ruby uses ruby_xmalloc() • which is wrapper around malloc()

Slide 47

Slide 47 text

RUBY_GC_MALLOC_LIMIT

Slide 48

Slide 48 text

Thank you! • https://github.com/code-mancers/rbkit • https://github.com/code-mancers/rbkit- client • http://thorstenball.com/blog/2014/03/12/ watching-understanding-ruby-2.1-garbage- collector/ • http://tmm1.net/ruby21-rgengc/

Slide 49

Slide 49 text

@gnufied http://www.codemancers.com Hemant Kumar