Confusion with Ruby's `GC#start` -


program - i

p ruby_version  = "a" b = "b"  p "#{a}" p "#{b}"  p "garbage count => #{gc.count}"  b = "d"  p "garbage count => #{gc.count}"  gc.start  p "garbage count => #{gc.count}"  p "#{a}" p "#{b}" 

output:

"1.9.3" "a" "b" "garbage count => 1" "garbage count => 1" "garbage count => 2" "a" "d" 

in program i output of p "garbage count => #{gc.count}" made me confused 1,1,2.confusion garbage objects count. tried modified version of program-ias below.

where commented out gc.start. looking @ output of program -ii seems strated gc.start in first program,which in turn destroyed no-reference object "b".

program - ii

p ruby_version  = "a" b = "b"  p "#{a}" p "#{b}"  p "garbage count => #{gc.count}"  b = "d"  p "garbage count => #{gc.count}"  #gc.start  p "garbage count => #{gc.count}"  p "#{a}" p "#{b}" 

output:

"1.9.3" "a" "b" "garbage count => 1" "garbage count => 1" "garbage count => 1" "a" "d" 

final questions are:

(a) before gc.start 1 object has been destryed clear output. object it?

(b) why no-reference object b not being destroyed without gc.start ?

edit

p gc.count p gc.disable p gc.count 

output

1 false 1 

in abive code have disabled garbage collection.why last gc.count showing output 1?

can me understand questions raised on mind stated above.

gc.count doesn't return number of garbage collected objects - returns number of times garbage collector has run, calling gc.start increment one.

a bunch of code gets executed before code starts run, it's not surprising garbage collection may occur.

disabling gc isn't going undo garbage collections have happened.


Comments