Nar
Nar

Reputation: 540

ruby on rails memory leak

rails 2.3.14, ruby 1.8.7

i have a rake task:

    Post.all.each do |post|

      post.comments.each { |pc|
        pc.delete
      }
      user = post.user

      pc = user.comment.build(
          :text => 'Test text',
      ......
      )
      result = pc.send_comment_http

      if result.success?
        pc.save!
      else
        puts error_message
      end
    end

Post have 100k row and task run 50h alredy, row are added every 3 seconds in db, it not problem, but ps -ax | grep rake indicates that occupied 2 gb. What could be the problem memory leak?

Upvotes: 1

Views: 718

Answers (2)

Andrey Kryachkov
Andrey Kryachkov

Reputation: 901

Use

Post.find_each do |post|
  ...    
end

Upvotes: 3

pablomarti
pablomarti

Reputation: 2107

The iterations on comments. The second loop you can avoid it by: post.comments.destroy_all

Also, when an user makes a comment, I can see it is not related to a post (because an user can have many posts, right?), so you can divide the operations. First truncate directly the comments table, second get the users that are related to a post (you can use join and group by, or just use join if you want that for each post sent a message), third loop over the users and send the message.

Upvotes: 3

Related Questions