Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created September 24, 2008 17:36
Show Gist options
  • Save tmm1/12617 to your computer and use it in GitHub Desktop.
Save tmm1/12617 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
GC.disable
class Test
Instances = {}
def initialize name
@name = name
Instances[name] = self
end
def remove secs = nil
self.class.delete(@name, secs)
end
def self.delete name, secs = nil
if secs
@delete_timers ||= {}
@delete_timers[name].cancel if @delete_timers.has_key? name
@delete_timers[name] = EM::Timer.new(secs){ delete name }
else
# WTF? uncomment either of the lines below, and the instance will get GC'd
# Instances[name] = nil
# p [Time.now, :DELETING, name]
Instances.delete(name)
end
end
end
def Test.check_objects
EM.add_periodic_timer(0.5) {
GC.enable
ObjectSpace.garbage_collect
GC.disable
c = 0
ObjectSpace.each_object(Test){ c+=1 }
p [Time.now, :OBJECTS, c]
}
end
def Test.run
Test.new('hello')
prc = Proc.new{ Test.delete('hello'); nil }
# EM.add_timer 0, prc
# EM.add_timer 0 do prc.call end
# EM.next_tick prc
# EM.next_tick do prc.call end
# prc.call
end
def Test.run2
t = Test.new(:abc)
# t.remove
t.remove(0)
end
EM.run{
Test.check_objects
Test.run2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment