Saturday, May 22, 2010

GoRuCo Pace U. - Session 2

"memprof: the ruby level memory profiler" - Aman Gupta

Addressing the ruby GC is a way to get Ruby to run faster. (Aman is speaking of MRI Ruby, which is the most commonly one used.)

When the GC is running, it "stops the world." None of your code runs while this happens. The GC is conservative, which makes it difficult to tweak. It also utilizes a design decision called "mark and sweep" which is a less-than-optimal one.

To improve performance:
- avoid leaked references: you create an object and you don't need it anymore, but you're still holding onto the reference - the GC will not clean it up but always has to look at it.

- create fewer objects: obviously

The tools:

1. ObjectSpace.each_object: When it's sorted and printed out you can get a sense of what's going on in the VM. An example:

types = Hash.new(0)
ObjectSpace.each_object do |obj|
types[obj.class] += 1
end

pp types.sort_by{ |klass, num| num }

2. gdb.rb: gdb hooks for REE. Written by Aman. It's "a bunch of python code" that understands how your ruby code works. gdb can find and fix leaks and has a number of different processes.

3. bleak_house: installs a custom patched version of ruby and tells you what is leaking and WHERE the leak is happening. The drawback is that you have to reinstall ruby and all of your gems. Aman also wrote a patch that includes more verbose information about the leak and where it's happening. It is a "heap dumping" patch.

4. memprof. ease of use, detailed information, and simple analysis. Thus it allows processing via various languages and databases using simple JSON data format. You can follow all the under-the-hood specifics on www.timetobleed.com.

MemProf.track: like bleak_house but only for a given block of code (not the entire code base). So we can drill down and get more specific into areas of your code base.

** You'll want to try these tools out in production mode. Development mode has more overhead and your results will be skewed **

memprof.com: a web-based visualizer and leak analyzer. Aman used it to find a leak in the development mode of Rails 3 beta.

No comments:

Post a Comment