Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debugging Memory Leaks in Ruby

Debugging Memory Leaks in Ruby

Reviewing the free memory analysis tools for MRI and JRuby (and mentioning Rubinius)

Jonathan Wallace

December 15, 2011
Tweet

More Decks by Jonathan Wallace

Other Decks in Programming

Transcript

  1. Get to know me! • software developer (Ruby, Rails, C,

    etc) • http://highgroove.com • [email protected] • @jonathanwallace • WE’RE HIRING!! 2 Wednesday, December 14, 11 background, schooling, etc
  2. What is a memory leak?? 3 Wednesday, December 14, 11

    before you can know what a memory leak is, you gotta know..
  3. How memory works 4 Wednesday, December 14, 11 quick recap,

    stack. dynamically allocated memory is done in the heap. that means, it lives between function calls. now, in ruby, all memory is allocated on the heap (even local variables): http://timetobleed.com/garbage-collection-slides-from-la-ruby-conference/
  4. So you think you have a leak? How would you

    know? 5 Wednesday, December 14, 11
  5. 8 Wednesday, December 14, 11 So you get a bright

    idea. maybe Its a memory leak!
  6. But how? 11 Wednesday, December 14, 11 ask the crowd

    how can you tell if you have a memory leak? examine the change in memory over time, if it goes up but doesn’t come down, then you may have a memory leak
  7. know your rubies 13 Wednesday, December 14, 11 First, determine

    which version of ruby you're using. THIS IS IMPORTANT. You can't efficiently track down a memory leak without a tool. And you can't choose which tool unless you know which version of ruby you're using.
  8. Limitations • Memprof - 64 bit only, 1.8.* MRI and

    REE • jruby - gems w/native extensions but no java equivalent • rubinius - maturity of the tools 15 Wednesday, December 14, 11
  9. Memprof w/RVM • "should *just work*" according to http:// beginrescueend.com/integration/memprof/

    • middleware (even something for rails 2.2) 16 Wednesday, December 14, 11 json dump of ruby objects, can be loaded into mongodb, also have memprof.com
  10. Eventually, I found: http://stackoverflow.com/a/ 7947504/91029 17 Wednesday, December 14, 11

    you can’t do this in the gem, only after the gem is installed
  11. VisualVM • $ rvm install jruby • $ jruby -J-Djruby.reify.classes=true

    script/ rails s • $ jvisualvm -J-mx2048m 23 Wednesday, December 14, 11 hash syntax issues
  12. Let’s debug a memory leak! • take a snapshot •

    make some requests against the app • take another snapshot • compare 24 Wednesday, December 14, 11
  13. Image Attributions • cc licensed ( BY SD ) flickr

    photo by Harry Wood: http://flickr.com/photos/harrywood/4248316700/ • cc licensed ( BY NC ) flickr photo by Fredrik Linge: http://flickr.com/photos/25172838@N05/4074004633/ • cc licensed ( BY NC ) flickr photo by daveograve@: http://flickr.com/photos/daveograve/581608282/ • cc licensed ( BY NC SD ) flickr photo by Automotivespace: http://flickr.com/photos/fam_montagna/6177485590/ • cc licensed ( BY NC SD ) flickr photo by RealEstateClientReferrals: http://flickr.com/photos/realestateclientreferrals/4049366729/ • cc licensed ( BY ) flickr photo by ilamont.com: http://flickr.com/photos/ilamont/5538511847/ • cc licensed ( BY NC ND ) flickr photo by MissBlythe: http://flickr.com/photos/missblythe/1484248550/ • cc licensed ( BY NC SD ) flickr photo by nathangibbs: http://flickr.com/photos/nathangibbs/98592171/ • cc licensed ( BY ND ) flickr photo by Caveman Chuck Coker: http://flickr.com/photos/caveman_92223/3347745000/ • Manup: With permission from creator: http://www.liambrazier.com/img/manup.jpg • cc licensed ( BY ND ) flickr photo by owlhere: http://flickr.com/photos/owlhere/5266992389/ • cc licensed ( BY NC SD ) flickr photo by ZakVTA: http://flickr.com/photos/isaachsieh/5457647194/ • cc licensed ( BY NC ) flickr photo by thisisfurious: http://flickr.com/photos/febhorne/3100226289/ 37 Wednesday, December 14, 11
  14. Further Reading and Credits • http://timetobleed.com/garbage-collection-slides-from-la-ruby-conference/ • http://www.shopify.com/technology/4321572-most-memory-leaks-are-good • http://rejeev.blogspot.com/2009/04/analyzing-memory-leak-in-java.html

    • http://www.engineyard.com/blog/2010/monitoring-memory-with-jruby- part-1-jhat-and-visualvm/ • http://rubini.us/doc/en/tools/memory-analysis/ • http://github/ice799/memprof 38 Wednesday, December 14, 11