Skip to content

Instantly share code, notes, and snippets.

@thehappycoder
Created March 7, 2013 10:08
Show Gist options
  • Save thehappycoder/5106953 to your computer and use it in GitHub Desktop.
Save thehappycoder/5106953 to your computer and use it in GitHub Desktop.
celluloid and ObjectSpace.each_object
require 'celluloid'
class Child
include Celluloid
include Celluloid::Logger
def start(cycle, n)
info "#{cycle}:#{n} started..."
if n == 2
Kernel.system('sleep', '15')
else
Kernel.system('sleep', '5')
end
info ''
info "#{cycle}:#{n} ended..."
end
end
class Master
include Celluloid
include Celluloid::Logger
def start
children = Child.pool(size: 2)
garbage_man = GarbageMan.new
cycle = 0
while cycle != 5 do
cycle += 1
garbage_man.collect
# garbage_man.collect! in order not to wait for the slow child#2
info '#' * 20
info "Running cycle ##{cycle}"
(1..2).each do |n|
children.start!(cycle, n)
end
sleep 10
end
end
end
class GarbageMan
include Celluloid
include Celluloid::Logger
def collect
#debug 'Collecting garbage'
GC.start
#debug 'Done collecting garbage'
counts = Hash.new{ 0 }
debug 'Collecting info from ObjectSpace'
ObjectSpace.each_object do |o|
#debug "Collecting #{o.class}" if o.is_a?(Child)
counts[o.class] += 1
#debug "Done for #{o.class}" if o.is_a?(Child)
end
debug 'Done collecting info'
counts = counts.sort {|a,b| b[1] <=> a[1] }
counts.each do |klass, count|
#info "#{klass} #{count}"
end
end
end
Master.new.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment