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)

D3630c5a12aa3e85748670729444d5a9?s=128

Jonathan Wallace

December 15, 2011
Tweet

Transcript

  1. Debugging Memory Leaks in Ruby 1 Wednesday, December 14, 11

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

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

    before you can know what a memory leak is, you gotta know..
  4. 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/
  5. So you think you have a leak? How would you

    know? 5 Wednesday, December 14, 11
  6. is slow 6 Wednesday, December 14, 11

  7. Customers 7 Wednesday, December 14, 11 angry customers

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

    idea. maybe Its a memory leak!
  9. but wait! no, instead of people its for 9 Wednesday,

    December 14, 11
  10. BE A HERO 10 Wednesday, December 14, 11

  11. 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
  12. Paid Options 12 Wednesday, December 14, 11

  13. 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.
  14. Free tools • Memprof, http://memprof.com • VisualVM, http://www.engineyard.com/blog/2010/monitoring- memory-with-jruby-part-1-jhat-and-visualvm/ •

    Rubinius, http://rubini.us/doc/en/tools/memory-analysis/ 14 Wednesday, December 14, 11
  15. 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
  16. 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
  17. 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
  18. # run memprof --pid 65421 --name 'test' --key <your_key_here>-- put-my-data-on-the-internet

    18 Wednesday, December 14, 11 what did that get me?
  19. 19 Wednesday, December 14, 11

  20. 20 Wednesday, December 14, 11

  21. 21 Wednesday, December 14, 11 Memprof::Middleware, Memprof::Tracer

  22. 22 Wednesday, December 14, 11 The Broken Promises of MRI/REE/YARV

  23. VisualVM • $ rvm install jruby • $ jruby -J-Djruby.reify.classes=true

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

    make some requests against the app • take another snapshot • compare 24 Wednesday, December 14, 11
  25. 25 Wednesday, December 14, 11

  26. 26 Wednesday, December 14, 11

  27. 27 Wednesday, December 14, 11

  28. 28 Wednesday, December 14, 11

  29. 29 Wednesday, December 14, 11

  30. 30 Wednesday, December 14, 11

  31. 31 Wednesday, December 14, 11

  32. 32 Wednesday, December 14, 11

  33. 33 Wednesday, December 14, 11

  34. 34 Wednesday, December 14, 11

  35. 35 Wednesday, December 14, 11

  36. Rubinius • http://rubini.us/doc/en/tools/memory- analysis/ 36 Wednesday, December 14, 11

  37. 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
  38. 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
  39. THANKS Questions? 39 Wednesday, December 14, 11