On 7/5/07, snacktime [email protected] wrote:
On 7/5/07, Pat M. [email protected] wrote:
I’ve got an app that is eating up lots of memory. When it first
starts up, it uses ~125 megs for a mongrel process, which isn’t
terrible I suppose.
That’s huge, are you sure that’s ram and not virtual memory?
I’m checking it out on my MBP, watching the process with the Activity
Monitor tool
When I first start it up, it uses 32 megs of Real Memory, 29 of
Private, and 77 of Virtual.
However after being used for a while it balloons
to 250-350 megs. It would appear there’s a big memory leak somewhere,
but I have no clue where to even begin looking. Any ideas?
Pin down when it started and then find out what changed at that time.
That’s where I would start. Normally I have at least an idea of where
something like that is coming from. Do you have large data sets you
are processing in some way, or retrieving through activerecord?
Here’s one line where I found something weird:
Company.top(10).collect {|c| c.complete_videos.size}
class Company < ActiveRecord::Base
def self.top(n)
find_by_sql [“SELECT companies.*, COUNT(videos.company_id) AS
num_videos FROM companies LEFT JOIN videos ON
companies.id=videos.company_id WHERE videos.status=‘complete’ AND
videos.deleted_at IS NULL GROUP BY companies.id, companies.name,
companies.nickname, companies.plan_id, companies.deleted_at ORDER BY
num_videos DESC LIMIT ?”, n]
end
def complete_videos(options = {})
@complete_videos ||= Video.find :all, { :conditions =>
“company_id=#{id} AND status=‘complete’” }.merge(options)
end
end
When I run that line in console, I get the proper results, and there’s
no memory spike.
One thing that’s really weird though is that if I run
2.times { Company.top(10).collect {|c| c.complete_videos.size} }
then it jumps to 48/45/93 and never comes back down.
My only guess is that the companies and videos are kept in a closure
and not released.
In the rails app itself, the offending code is:
<% @top_companies.each do |c| %>
<%= link_to c.name, company_url(c) %><%=
c.complete_videos.size %>
<% end %>
I’m not doing anything like 2.times… there.
One thing I have checked out is
When I collect the top company’s video sizes, it increases the
number of Video objects by 5689, but I never see it come back down.
I just noticed that if I run
Company.find(2).complete_videos.size
twice in quick succession, the memory jumps up to the high 49/46/94.
So maybe it has to do with how quickly a connection is being made to
the database?
I’m using Rails 1.2.3, Ruby 1.8.6, and PostgreSQL 8.2.3.
Pat