Reputation: 540
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
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