Last active
August 29, 2015 13:58
-
-
Save ryanttb/9935380 to your computer and use it in GitHub Desktop.
Using Thread to help ruby garbage collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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