Skip to content

Instantly share code, notes, and snippets.

@swistak35
Created July 18, 2012 19:01
Show Gist options
  • Save swistak35/3138102 to your computer and use it in GitHub Desktop.
Save swistak35/3138102 to your computer and use it in GitHub Desktop.
Playing with ObjectSpace and GC
class Vehicle
class << self
attr_accessor :count
def finalize(id)
@count -= 1
end
def all #returns an array of all Vehicle objects
ObjectSpace.each_object(Vehicle).to_a
end
end
Vehicle.count ||= 0
def initialize
Vehicle.count += 1
ObjectSpace.define_finalizer(self, Vehicle.method(:finalize))
end
end
arr = []
100.times { arr << Vehicle.new }
arr.each do |a|
arr.delete(a)
end
arr.size #=> Raz zostaje 25, raz 50...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment