Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory grows indefinitely in an empty Rails app

I can't figure out why my Rails app (hosted on Heroku (cedar)) keeps allocating more and more memory. If I didn't know any better I'd say that this a memory leak in Ruby/Rails, but since I'm completely new to Ruby/Rails, I feel like I'm missing something completely obvious.

I'm using Rails defaults generated by rails new, and completely up-to-date gems:

source 'https://rubygems.org'

gem 'rails', '3.2.8'

group :development do
  gem 'sqlite3'
end

group :assets do
  gem "sass-rails", "~> 3.2.5"
  gem "coffee-rails", "~> 3.2.2"
  gem "uglifier", "~> 1.3.0"
end

gem "jquery-rails", "~> 2.1.2"

group :production do
  gem 'newrelic_rpm'
  gem "pg", "~> 0.14.1"
end

I'm using the default newrelic config. I have zero models and one controller, nothing_controller.rb, which was generated using rails generate controller nothing:

class NothingController < ApplicationController
  def index
  end
end

I deleted public/index.html and added an empty views/nothing/index.html.erb. The only other thing I did to the generated app was add a route to routes.rb:

Nothing::Application.routes.draw do
  root :to => "nothing#index"
end

I committed, pushed it to Heroku, then wrote a quick script that would load my Heroku page every 10 seconds. This is what my New Relic reports:

http://i.imgur.com/djZZn.png

That's all there is to it. Memory just keeps increasing like that until it passes Heroku's limit of 512MB. The code in this app is pretty much the same as the code I've seen in the tutorial I followed. I don't understand what I'm doing wrong.

Any guidance would be greatly appreciated.

EDIT (9/12): In case it's relevant, I'm using ruby 1.9.

Code I'm using to hit the server (C#):

using (var wc = new WebClient())
  for (;; Thread.Sleep(10000))
    wc.DownloadString("http://vast-earth-9859.herokuapp.com/");

EDIT (9/13): Going to try disabling New Relic and seeing if it still R14s.

like image 706
user1650177 Avatar asked Sep 12 '12 22:09

user1650177


2 Answers

Trying removing newrelic gem. I had have same issue on heroku and culprit was newrelic gem. You can check your current object that is memory. Below code show you the object count.

ObjectSpace.each_object.with_object(Hash.new(0)){|obj, h| h[obj.class] +=1 }.select{|k,v| puts "#{k} => #{v}" if k == String || k == Array || k == Hash}
like image 168
MKumar Avatar answered Oct 14 '22 13:10

MKumar


Once the memory exceeds(512 MB) the limit on heroku, it won't get the memory down immediately by restarting the app or by changing code. It will keep on growing and slows it further, heroku itself kills the process when it hits heroku's memory threshold (not sure how much it is).

There are various things might have caused the memory leaks, following are the few 1. Continuous exceptions thrown 2. Getting lot of data in a single request.

Testing it on local machine won't help since you won't have any memory restriction on your machine.

Do the following to see the heroku log and figure what are the latest errors what are items take time.

heroku logs -a <application name> -n<number of lines>

Stop the application for day or so then you can the change code and start the application.

like image 2
Sethupathi Avatar answered Oct 14 '22 11:10

Sethupathi