Armin Ronacher

Stupid Ruby Leak

written by Armin Ronacher, on Thursday, May 22, 2008 10:21.

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

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

Comments

  1. wow!

    —  EAL on Thursday, May 22, 2008 18:42 #

  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?

    —  Robin Munn on Thursday, May 22, 2008 21:36 #

  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.

    —  Armin Ronacher on Thursday, May 22, 2008 22:05 #

  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.

    —  Robin Munn on Friday, May 23, 2008 22:34 #

  5. "leak while true". Ruby definitely has the best syntax, ever.

    —  Martin on Friday, May 23, 2008 22:57 #

  6. Hehe, I recorded a video to demostrate this:
    humitos.wordpress.com/2008/05/26/ruby-and-python/

    —  humitos on Monday, May 26, 2008 16:05 #

Leave a Reply