[Ruby] Ghetto

javier ramirez jramirez at aspgems.com
Thu Jan 3 00:48:45 PST 2008


Hi,
> Ruby has a conservative GC.  This means it won't necessarily ever kill
> an object, even if all references are gone.
>   
the above is true, and in this case you are doing something a bit weird 
and probably the GC is not sure how to proceed, so it will act 
conservative. You are creating an instance of user without assigning it 
to any variable, so there will not be references to the object_id 
associated to any variables for the GC to check. My guess is that's 
probably causing a bit of confussion for the GC.

I just checked if you modify your code and assign the new object to a 
variable, then the GC will do his part as expected

class User; end
user = User.new  #here i assign the new object to a variable. Reference count for the object_id will be increase
user = nil	 #here we are decreasing the reference count for that object

GC.start	# reference count for that object_id should be 0 by now, so GC will remove it (probably, GCs have a strong personality ;)  )

ObjectSpace.each_object do |object|
  user = object if object.class.to_s == 'User'
end
p user  	#we get a nil here, as expected



regards,

javier ramírez


More information about the Ruby mailing list