So I ran into this memory leak about a year ago but had forgotten about it so I’m going to document it in the hopes I check my own blog next time I find it. I came across the leak again when looking into why one of my apps was getting so bloated after running for a few days. The leak has been reported in numerous places but doesn’t appear to have been patched in 1.8.6.
This should demonstrate it:
# Just a helper method to show the memory usage output # @NOTE: Won't work on Windows def log leak='fix' ps = %x(ps u -p #{Process.pid}).strip.split(/\n/).last.split(/\s+/) puts "#{ps[4]} #{ps[5]}" end # This leaks memory def bad "ruby+memory+leak".split('+') end # Defining a variable before the String#split # fixes the leak def good rm = '+' "ruby+memory+leak".split(rm) end puts "VSZ RSS" 500_000.times do |i| good log if i%100000 == 0 end puts "\nWatch me leak!" 500_000.times do |i| bad log if i%100000 == 0 end
So the moral of the story is, make sure to define a variable in methods that use String#split, String#gsub and the like. This doesn’t leak in ruby 1.8.4. I haven’t checked 1.9, but I’ve heard it’s fixed there too.
Here is a more complete script demonstrating the issue with both class and instance methods (since some reporters have mentioned it being strictly a class method problem).




5 comments so far
This was fixed in 1.8.7.
January 17th, 2009 at 12:18 am
Thanks, Ed. I’ll look into 1.8.7.
January 17th, 2009 at 12:10 pm
I was wondering what version of ruby 1.8.6 did you use? It appears 1.8.6 p230-p238 had a problem with memory leaks: http://redmine.ruby-lang.org/issues/show/216 I’m wondering if this issue is related or not.
June 17th, 2009 at 1:58 pm
Alex,
Yes - that looks to be the same bug. I was using 1.8.4.
June 17th, 2009 at 3:07 pm
[…] Ruby 1.8.6 leaks memory in some surprising places. Even gsub and split on the String class cause some bad headaches. If you’re using Haml and 1.8.6 - you are probably in a bit of trouble. Read more: http://blog.edhickey.com/2008/12/03/memory-leak-in-ruby-186-string-class/ […]
September 4th, 2009 at 9:07 am
Leave a Reply