Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a memory leak on Heroku?

I have a Rails 3.2.8 app running on Heroku Cedar with Ruby 1.9.3. The app runs fine when it launches but after a day or so of continuous use, I start to see R14 errors on my logs. Once the memory errors start, they never go away, even if the app is idle for several hours.

Shouldnt the garbage collector clean up unused objects after a while and reduce the memory load? It seems this is not happening on Heroku. Generally, memory usage starts to creep up after running some reports with several thousand rows of data, although results are paginated.

How can I find the memory leak? Plugins like bleak_house are way out of date or dont run nicely in the Heroku environment. Can I adjust the GC settings to make it more aggressive?

like image 500
Marty M. Avatar asked Nov 09 '12 16:11

Marty M.


People also ask

How do you detect a memory leak?

The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.

How do I fix heroku memory leak?

Pruning your Gemfile helps to decrease your starting memory. You can think of this as the minimum amount of RAM needed to boot your app. Once your app actually starts responding to requests, memory will only go up.

How do I check heroku memory usage?

Heroku has a log-runtime-metrics feature to monitor the memory usage now. and the memory usage will be shown on the server log. Read Heroku log-runtime-metrics docs for more information. Save this answer.


1 Answers

The GC should do the clean up, and probably does.

You can force the GC with GC.start; if many objects were not collected this will, but I suspect that is not the issue.

Is it possible you somehow create a bunch of objects and never release them, by keeping cached copies or something?

I'm unfamiliar with the existing tools to check this, but you may want to check which objects exist using ObjectSpace. For example:

ObjectSpace.each_object.with_object(Hash.new(0)){|obj, h| h[obj.class] +=1 }
# => a Hash with the number of objects by class

If you get an unexpected number for one of your classes, for instance, you would have a better idea of where to look for.

like image 138
Marc-André Lafortune Avatar answered Oct 22 '22 23:10

Marc-André Lafortune