E
Eustaquio Rangel de Oliveira Jr.
Hi.
I have a doubt about garbage collection:
-------------------------------------------------------------------------=
---------------
class CustomObject
attr_accessor :val, :next
def initialize(v,n=3Dnil)
@val =3D v
@next =3D n
end
def to_s
"Object #{@val} (#{self.object_id}) points to #{@next.nil? ? 'nothing' =
:=20
@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=20
found\n\n"
end
c1 =3D CustomObject.new("1",CustomObject.new("2",CustomObject.new("3")))
c4 =3D CustomObject.new("4",CustomObject.new("5"))
c6 =3D CustomObject.new("6")
c1.next.next.next =3D c1 # comment this and check again
list
c1 =3D nil
c4.next =3D 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=
=20
1), 2 points to 3 (created internally on 2), and 3 points to c1, which is=
a=20
reference at root (?) level there and again points to an internal 2, that=
=20
points to 3, that points to c1 ...
My question is, when assigning nil to c1, it should not invalidate all th=
e=20
inner objects and becomes available for garbage collection?
On the example above, I assigned nil to c1 and c4.next (5), and after the=
=20
GC.start I don't have 5, but still have c1 (same id), 2 and 3. On that=20
case, that memory will never be sweeped (free)? Because seems that I'll=20
always have a Object with id -604874856 and will not have a way to refer =
to=20
it later, for use or free the allocated memory for it.
Thanks.
I have a doubt about garbage collection:
-------------------------------------------------------------------------=
---------------
class CustomObject
attr_accessor :val, :next
def initialize(v,n=3Dnil)
@val =3D v
@next =3D n
end
def to_s
"Object #{@val} (#{self.object_id}) points to #{@next.nil? ? 'nothing' =
:=20
@next.val} (#{@next.nil? ? ''
end
end
def list
print "Listing all CustomObject's with ObjectSpace\n"
print "#{ObjectSpace.each_object(CustomObject) {|v| puts v}} objects=20
found\n\n"
end
c1 =3D CustomObject.new("1",CustomObject.new("2",CustomObject.new("3")))
c4 =3D CustomObject.new("4",CustomObject.new("5"))
c6 =3D CustomObject.new("6")
c1.next.next.next =3D c1 # comment this and check again
list
c1 =3D nil
c4.next =3D 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=
=20
1), 2 points to 3 (created internally on 2), and 3 points to c1, which is=
a=20
reference at root (?) level there and again points to an internal 2, that=
=20
points to 3, that points to c1 ...
My question is, when assigning nil to c1, it should not invalidate all th=
e=20
inner objects and becomes available for garbage collection?
On the example above, I assigned nil to c1 and c4.next (5), and after the=
=20
GC.start I don't have 5, but still have c1 (same id), 2 and 3. On that=20
case, that memory will never be sweeped (free)? Because seems that I'll=20
always have a Object with id -604874856 and will not have a way to refer =
to=20
it later, for use or free the allocated memory for it.
Thanks.