Very strange GC behaviour

(Geert Fannes) #1

Hello, can someone explain why GC fails to release "a" in the program
hereunder. When I exchange the lines "a=A.new" and "b=B.new", GC fails
to release b.

PROGRAM

#create some dummy classes

class A;end

class B;end

#create two variables, one for each dummy class

a=A.new

b=B.new

#store the object_id's for these object to check if these are still
present

bid=b.object_id

aid=a.object_id

#check which objects can still be found

ObjectSpace.each_object{|obj|puts("a still found") if
obj.object_id==aid;puts("b still found") if obj.object_id==bid}

puts("")

#set both objects to nil and do garbage collection

b=nil

a=nil

GC.start

#check again which object are still present. Both a and b should be
gone, but that is not the case

ObjectSpace.each_object{|obj|puts("a still found") if
obj.object_id==aid;puts("b still found") if obj.object_id==bid}

OUTPUT:

gfannes@ivon gruby $ ruby test.rb

b still found

a still found

a still found

gfannes@ivon gruby $

Greetings,

Geert

(Brian Schröder) #2

The Garbage Collector is conservative. If the address of an object
stays in some register, or even some calculation filled a register
with a number that happens to equal the address, the pointed-to object
will not be gc'ed.

regards,

Brian

···

On 23/08/05, Geert Fannes <Geert.Fannes@ikanconsulting.com> wrote:

Hello, can someone explain why GC fails to release "a" in the program
hereunder. When I exchange the lines "a=A.new" and "b=B.new", GC fails
to release b.

PROGRAM

#create some dummy classes

class A;end

class B;end

#create two variables, one for each dummy class

a=A.new

b=B.new

#store the object_id's for these object to check if these are still
present

bid=b.object_id

aid=a.object_id

#check which objects can still be found

ObjectSpace.each_object{|obj|puts("a still found") if
obj.object_id==aid;puts("b still found") if obj.object_id==bid}

puts("")

#set both objects to nil and do garbage collection

b=nil

a=nil

GC.start

#check again which object are still present. Both a and b should be
gone, but that is not the case

ObjectSpace.each_object{|obj|puts("a still found") if
obj.object_id==aid;puts("b still found") if obj.object_id==bid}

OUTPUT:

gfannes@ivon gruby $ ruby test.rb

b still found

a still found

a still found

gfannes@ivon gruby $

Greetings,

Geert

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/