Beginner's question about GC

R

Ron M. Newman

Hi,

Let's say I have an object A.

Object A exclusively references object B.

Now: Object A goes out of scope.

does this mean the system garbage collects object A and B together?

I think it should. Am I correct?

Thanks
-Ron
 
M

Marina Levit [MVP]

I'm not sure what you mean by 'together'. But yes, both A and all of it's
B's (as long as nothing else is holding on to reference to the B's) would be
eligible for garbage collection. That doesnt' mean they will be garbage
collected immediately - just when the GC decides to run.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

There are other factors that might affect when the objects are
collected, like size and age of the objects. But, yes, the GC will
recognise both of the objects as up for collection at the same run
(unless they are in different generations, and the garbage collection
run only considers the first generation).

When the garbage collector runs it starts out by considering all objects
as collecteable, then looks through all references to exclude objects
that aren't collectable. It doesn't look for references in objects that
are collectable, so as A is collectable, the reference to B doesn't keep
B from being collectable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top