Search code examples
ruby-on-railsrubygarbage-collectionsidekiq

Clear memory after some number of records passed


I have few m of records in db, and need to process it from time to time. However this operation takes all memory on my server. I'm running this operation using sidekiq. So while this task using all memory, my rails app becomes very slow.

In general(no logic included) my code looks like

Model.each do |m|
//do some logic code here
end

How do i make garbage collector to run after some amount of records(for ex. 10k records) so i wouldn't face out of memory situations. Will splitting it in chunks help me?


Solution

  • You should always use find_each when dealing with potentially large tables.

    That way, models will be retrieved from the database and loaded in memory batch by batch (the default size is 1000 but you can customize it to your needs).

    Just be aware that sorting by arbitrary columns doesn't play well with find_each, as it implicitly sorts records by ID so that it has a way to fetch records by batches.