Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I prevent my Rails query from maxing out memory
    text
    copied!<p>I'm sshing into a server running my Rails app in order to run queries. Despite using <code>find_each</code>, the server's memory is getting eaten up by my console session and I'm not sure why. <a href="https://stackoverflow.com/questions/6668875/ruby-on-rails-memory-leak-when-looping-through-large-number-of-records-find-eac/6682326#6682326">Based on this SO answer</a>, I'm also using <code>ActiveRecord::Base.uncached</code> to prevent ActiveRecord from caching all the objects and eating up memory.</p> <p><em>Edit:</em> Figured it out, thanks to @user2864740 and Micah Fivecoate. The issue was with sending off tasks to Resque, which I'm doing with the Resque Delayable gem (See answer below).</p> <p>Here's what I'm doing:</p> <pre><code>ssh cron.ec2.company-name.com cd /var/www/company-name.com/current; bundle exec rails console production </code></pre> <p>Then, in the console:</p> <pre><code>ActiveRecord::Base.uncached do ModelObject.find_each do |obj| a = AnotherModelObject.find_by_model_id(obj.id) a ||= AnotherModelObject.create!(obj.id) a.send_task_to_resque end end </code></pre> <p>As soon as I kill my SSH session, the memory is freed up.</p> <p>I'm running Rails 3.2.15 with ruby 1.9.3p327 on Linux.</p> <p>I'm getting the text of all the SQL queries sent back to me over the SSH session—maybe this is kept in memory or something? Next I might try with <code>conf.echo = false</code> and <code>ActiveRecord::Base.logger.level = 1</code>. <em>Edit: This didn't fix the memory usage</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload