Christoffer
Christoffer

Reputation: 2411

Major factors for memory leaks in Rails

I am tryin to resolve a memory leak problem with Rails. I can see through New Relic that the usage of memory is increasing without ever decreasing.

This is a spinoff question from a large thread ( Memory constantly increasing in Rails app ) where I am trouble shooting the problem. What I need to know now is just:

What are major reasons/factors when it comes to memory leaks in Rails?

As far as I understand:

I want to use this information to look through my code so please provide examples!

Lastly, would this be "memory leaking code"?

ProductController
...
@last_products << Product.order("ASC").limit(5)
end

will that make @last_products bloat?

Upvotes: 7

Views: 1428

Answers (1)

rovermicrover
rovermicrover

Reputation: 1453

The following will destroy applications.

Foo.each do |bar|
  #Whatever
end

If you have a lot of Foos that will pull them all into memory. I have seen apps blow up because they have a bunch of "Foos" and they have a rake task that runs through all the foos, and this rake task takes forever, lets say Y seconds, but is run every X seconds, where X < Y. So what happens is they now have all Foos in memory, more than once because they just keep pulling stuff into memory, over and over again.

While this can't exactly happen inside an front facing web app in the same way it isn't exactly efficient or wanted.

Instead of the above do the following

Foo.find_each do |bar|
  #Whatever
end

Which retrieves things and batches and doesn't put a whole bunch of stuff into your memory all at once.

And right as I finished typing this I realized this question was asked in September of last year... oh boy...

Upvotes: 8

Related Questions