Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak in Rails App... string nightmare

So I've got this rails app with a horrible memory leak problem. I'm running it on Heroku, and for the last couple days while I've been trying to figure out what on earth is causing it, I've had to continually restart the app every 5 hours or so or the memory exceeds 512 and my app crashes.

I'm using Oink, and I'm not seeing anything fishy with my instantiated classes. The only thing is that with each page load, the memory used creeps up. It doesn't even matter which page I load, it's seemingly random.

New Relic hasn't been much help either, since it doesn't have to do with any one particular controller action.

The only thing I've been able to find was using ObjectSpace, which revealed an ENORMOUS number of string objects in memory, and the number just grows and grows. I used a little tool to log the value of the strings and they're all blank. Not nil... but whitespace. My production app regularly has over 200,000 string objects in use.

Does anyone have any idea what these could possibly be coming from? This is really stressing me out having to be at my computer every five hours to keep my site alive.

Thanks!

like image 843
Stephen Corwin Avatar asked Nov 17 '12 08:11

Stephen Corwin


People also ask

Can you stack memory leaks?

No it is not possible to create a memory leak with objects that have automatic storage duration (some people call this stack). When you have a variable with automatic storage duration it is cleaned up by the program automatically.

Does exit cause memory leaks?

Not under modern operating systems, no. The OS automatically collects all the memory when the process dies. In fact freeing memory can actually be detrimental for the performance if the program is exiting anyway.


Video Answer


1 Answers

Okay, this is absolutely ridiculous, but after two weeks of anxiety and struggle, I found the solution to this leak. It literally all came down to this one line:

ActionMailer::Base.delivery_method = :smtp

in my application.rb config file.

It should have been:

config.action_mailer.delivery_method = :smtp

I have no idea how I got it wrong, but boy did it wreak havoc on my app.

Does anyone know why this would cause such a problem? I was losing about a megabyte of memory on every single query. Crazy.

like image 110
Stephen Corwin Avatar answered Oct 02 '22 14:10

Stephen Corwin