From: "Eustaquio Rangel de Oliveira Jr." Date: 2005-07-07T06:13:50+09:00 Subject: Collecting garbage Hi. I have a doubt about garbage collection: ---------------------------------------------------------------------------------------- class CustomObject attr_accessor :val, :next def initialize(v,n=nil) @val = v @next = n end def to_s "Object #{@val} (#{self.object_id}) points to #{@next.nil? ? 'nothing' : @next.val} (#{@next.nil? ? '':@next.object_id})" end end def list print "Listing all CustomObject's with ObjectSpace\n" print "#{ObjectSpace.each_object(CustomObject) {|v| puts v}} objects found\n\n" end c1 = CustomObject.new("1",CustomObject.new("2",CustomObject.new("3"))) c4 = CustomObject.new("4",CustomObject.new("5")) c6 = CustomObject.new("6") c1.next.next.next = c1 # comment this and check again list c1 = nil c4.next = nil GC.start # here I want c1 disappears sleep(1) list ---------------------------------------------------------------------------------------- running this program I get ---------------------------------------------------------------------------------------- Listing all CustomObject's with ObjectSpace Object 6 (-604874926) points to nothing () Object 4 (-604874906) points to 5 (-604874896) Object 5 (-604874896) points to nothing () Object 1 (-604874866) points to 2 (-604874856) Object 2 (-604874856) points to 3 (-604874846) Object 3 (-604874846) points to 1 (-604874866) 6 objects found Listing all CustomObject's with ObjectSpace Object 6 (-604874926) points to nothing () Object 4 (-604874906) points to nothing () Object 1 (-604874866) points to 2 (-604874856) Object 2 (-604874856) points to 3 (-604874846) Object 3 (-604874846) points to 1 (-604874866) 5 objects found ---------------------------------------------------------------------------------------- There's a circular reference there: c1 points to 2 (created internally on 1), 2 points to 3 (created internally on 2), and 3 points to c1, which is a reference at root (?) level there and again points to an internal 2, that points to 3, that points to c1 ... My question is, when assigning nil to c1, it should not invalidate all the inner objects and becomes available for garbage collection? On the example above, I assigned nil to c1 and c4.next (5), and after the GC.start I don't have 5, but still have c1 (same id), 2 and 3. On that case, that memory will never be sweeped (free)? Because seems that I'll always have a Object with id -604874856 and will not have a way to refer to it later, for use or free the allocated memory for it. Thanks. ---------------------------- Eust�quio "TaQ" Rangel eustaquiorangel@yahoo.com http://beam.to/taq Usu�rio GNU/Linux no. 224050