Created
July 18, 2012 19:01
-
-
Save swistak35/3138102 to your computer and use it in GitHub Desktop.
Playing with ObjectSpace and GC
This file contains 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
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