memory leaks in ruby applications

20
Visual representations of memory issues in Ruby and Rails Camille Baldock, @camille_

Upload: camille-baldock

Post on 10-May-2015

1.287 views

Category:

Software


0 download

DESCRIPTION

Lightning talk about some solutions to finding, preventing memory leaks, churn and bloat. http://camillebaldock.co.uk/memory-leaks-ruby-applications/ Website: http://camillebaldock.co.uk Twitter: @camille_

TRANSCRIPT

Page 1: Memory leaks in Ruby applications

Visual representations of memory issues in Ruby

and RailsCamille Baldock, @camille_

Page 2: Memory leaks in Ruby applications
Page 3: Memory leaks in Ruby applications
Page 4: Memory leaks in Ruby applications

Bloat

• Memory usage has grown with data set

• Loading too many objects at once

Page 5: Memory leaks in Ruby applications

Memory leaks

• Objects that will never be released by the garbage collector

• Objects whose size grows the longer the process runs

Page 6: Memory leaks in Ruby applications

Memory churn

• Allocating and deallocating a LOT of objects

Page 7: Memory leaks in Ruby applications

• Oink

• Ruby object graphs

• A few best practices…

!

!

Page 8: Memory leaks in Ruby applications

Configuring Oink

• Rails.application.middleware.use( Oink::Middleware, :instruments => :memory )

!

• Rails.application.middleware.use( Oink::Middleware, :instruments => :activerecord )

Page 9: Memory leaks in Ruby applications

!

• ReportsController#show

• Max heap size: 157524 KB

• Number of AR objects: 14565

Page 10: Memory leaks in Ruby applications

Be proactive about

being lazy

Page 11: Memory leaks in Ruby applications

Where are the leaks ?

• Look at the object graph in memory !

• ObjectSpace.find_references

• A new ObjectGraph standard library

Page 12: Memory leaks in Ruby applications

Journey through the object graph

Page 13: Memory leaks in Ruby applications

Counting objects…

Page 14: Memory leaks in Ruby applications

Finding something horrible…

Page 15: Memory leaks in Ruby applications

!

• status?timestamp=110143244

• status?timestamp=110143245

• ….

Page 16: Memory leaks in Ruby applications

Stop using global state !

• Global variables

• Class variables

• Per-process instance state

• …

Page 17: Memory leaks in Ruby applications

My app is still slow…

Page 18: Memory leaks in Ruby applications

Sources of memory churn

Page 19: Memory leaks in Ruby applications

Mutation limits allocation

Page 20: Memory leaks in Ruby applications

• Twitter: @camille_

• Github: camilleldn

!

• Conrad Irwin, Object Graph exploration for Ruby memory leaks

• Oink gem, https://github.com/noahd1/oink

• Boston Ruby - Joe Ferris - Memory Issues in Rails