Stupid Ruby Leak

def leak
  "a string".split(/s+/)
end

leak while true

Workaround: assign the return value to a temporary variable and then return it.

6 Responses to “Stupid Ruby Leak”

  1. wow!

    Comment by EAL — Thursday, May 22nd, 2008 @ 6:42 pm
  2. I have zero experience with Ruby, so I’m a little lost here. Why, exactly, does this leak? And why does the workaround succeed?

    Comment by Robin Munn — Thursday, May 22nd, 2008 @ 9:36 pm
  3. It leaks because something in the regular expression module is not free()ing allocated memory. The garbage collector works, the object count is not increasing. Why exactly it leaks I don’t know, I don’t have the time to analyze that right now. However I filed a bug and I guess someone will fix it soon.

    Comment by Armin Ronacher — Thursday, May 22nd, 2008 @ 10:05 pm
  4. I see. I was picturing something along the lines of an object leak, e.g. something about returning the object directly (instead of assigning it to a temp variable) was causing the GC to not notice the object, thus leading to an increase in the object count. So my non-knowledge of Ruby wasn’t really relevant, since there isn’t anything weird going on with references or monkeypatching.

    Comment by Robin Munn — Friday, May 23rd, 2008 @ 10:34 pm
  5. “leak while true”. Ruby definitely has the best syntax, ever.

    Comment by Martin — Friday, May 23rd, 2008 @ 10:57 pm
  6. Hehe, I recorded a video to demostrate this:
    http://humitos.wordpress.com/2008/05/26/ruby-and-python/

    Comment by humitos — Monday, May 26th, 2008 @ 4:05 pm

Leave a Reply

cogitations driven by wordpress