I'm having trouble understanding when Ruby GC kicks in for Object
instances that are no longer referred to by anything. A simple code
sample to illustrate:
If you run this you'll see that 4 TestClass instances are still in the
ObjectSpace even after they are deleted from the collection array. I
tried manually calling the garbage collector after deleting the elements
from the array but I get the same result.
Ruby uses a conservative garbage collector which means that an object
won't be destroyed unless there is no value in the stack or registered
global memory location which contains a value that looks like a
pointer to a Ruby object. So it's therefore not possible to reliably
know when Ruby will decide an object is no longer reachable.
For example, the Array#delete_at function returns the value deleted so
while evaluating that expression the interpreter may leave the
intermediate result on the stack temporarily so it won't get reaped.
You can increase the chances of your object losing all references by
evaluating the code with a deep recursive stack as in the example
below. I don't recommend actually doing this in practice though, it's
better to just trust that Ruby will *eventually* get rid of your
objects.
On Thu, Aug 07, 2008 at 11:10:32PM +0900, Larsenmtl Larsenmtl wrote:
I'm having trouble understanding when Ruby GC kicks in for Object
instances that are no longer referred to by anything. A simple code
sample to illustrate:
Though I must admit it worries a bit for me to see people using
ObjectSpace to check memory consumption. To me, this either means
that folks are getting concerned about issues that they don't need to
worry about, or that there are larger problems in their code with
respect to memory consumption. I don't mean you here, just in
general.
-greg
···
On Thu, Aug 7, 2008 at 10:10 AM, Larsenmtl Larsenmtl <larsenmtl@gmail.com> wrote:
If you run this you'll see that 4 TestClass instances are still in the
ObjectSpace even after they are deleted from the collection array. I
tried manually calling the garbage collector after deleting the elements
from the array but I get the same result.