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.
the fruity and personal webpage of armin ronacher
def leak
"a string".split(/s+/)
end
leak while true
Workaround: assign the return value to a temporary variable and then return it.
wow!
Comment by EAL — Thursday, May 22nd, 2008 @ 6:42 pmI 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 pmIt 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 pmI 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“leak while true”. Ruby definitely has the best syntax, ever.
Comment by Martin — Friday, May 23rd, 2008 @ 10:57 pmHehe, I recorded a video to demostrate this:
Comment by humitos — Monday, May 26th, 2008 @ 4:05 pmhttp://humitos.wordpress.com/2008/05/26/ruby-and-python/