Josh Moore Josh Moore - 25 days ago 5x
Ruby Question

Ruby Memory Management

I have been using Ruby for a while now and I find, for bigger projects, it can take up a fair amount of memory. What are some best practices for reducing memory usage in Ruby?

  • Please, let each answer have one "best practice" and let the community vote it up.


Don't do this:

def method(x)
  x.split( doesn't matter what the args are )

or this:

def method(x)
  x.gsub( doesn't matter what the args are )

Both will permanently leak memory in ruby 1.8.5 and 1.8.6. (not sure about 1.8.7 as I haven't tried it, but I really hope it's fixed.) The workaround is stupid and involves creating a local variable. You don't have to use the local, just create one...

Things like this are why I have lots of love for the ruby language, but no respect for MRI