Skip to content

Instantly share code, notes, and snippets.

@ryanttb
Last active August 29, 2015 13:58
Show Gist options
  • Save ryanttb/9935380 to your computer and use it in GitHub Desktop.
Save ryanttb/9935380 to your computer and use it in GitHub Desktop.
Using Thread to help ruby garbage collection
# GC.start does not help here, the heap keeps growing
class Foo < ActiveRecord::Base
def self.process_all!
all.each { |record|
puts "Processing #{ record.id }"
record.high_mem_process
record.save!
GC.start
}
end
end
# GC.start cleans the heap after the thread exits
class Foo < ActiveRecord::Base
def self.process_one!( id )
puts "Processing #{ id }"
record = find id
record.high_mem_process
record.save!
end
def self.process_all!
ids = select :id
ids.each { |r|
t = Thread.new { process_one! r.id }
t.join
GC.start
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment