views:

335

answers:

9

I was wondering how the garbage collector in Java deals with the following situation.

Object A has a reference to Object B and Object B has a reference to Object C. The main program has a reference to Object A. So you can use Object B trough Object A, and Object C trough Object B trough Object A.

What happens to Object B and Object C, if the link between Object A and Object B is set to null?

Should Object B and Object C now been collected by the Garbage Collector? I mean there is still a connection between Object B and Object C.

+2  A: 

You can't count on the garbage collector to work at a specific time,since its behavior is unpredictable,all you can say is that objects B and C are only eligible for garbage collection

Phobia
+1  A: 

I think the logic is different. If the object is not accessible from a thread then it can be collected.

extraneon
+5  A: 

Yes, B and C are eligible for garbage collection, if they can't be reached from any GC root (GC roots are usually all Threads and all references on the stack).

Joachim Sauer
... and all static variables
Maurice Perry
@Maurice: not directly, as far as I know. static variables can be reached via the `Class` they belong to, which can be reached via the `ClassLoader` that loaded them, which can be reached via other classes that it loaded which can be reached via objects of that type. So if the `ClassLoader` is GCed, you can even lose the value of a static variable.
Joachim Sauer
You may well be right
Maurice Perry
@Joachim: Are static variables not part of a class and not of an object?
Bright010957
@Bright: yes they are. But the class can be GCed as well.
Joachim Sauer
@Joachim: See topic start, I added an extra question.
Bright010957
+5  A: 

Should Object B and Object C now been collected by the Garbage Collector?

Yes. Well, they are candidates for collection because there's no way to reach Object B and C through the root that is A.

This article might help you understand the Object Lifecycle and when an object is eligible for garbage.

bruno conde
+1  A: 

If there is no reference to object, then it will be suitable for GC to proceed

Artic
+2  A: 

In fact, garbage collection in java is a very sophisticated thing, far more than in Ruby interpreter, as an example.

Anyway, the theoretical basis is the same.

The GC identifies objects graphs that are no more reachable by program code (that's to say they have no more reference in active code). When talking about object graph, I precisely talk about B->C object graph. once it is unreachable, it can be GC'ed, but you can't tell when it will be, due to the GC trying to optimize as much as possible its work to avoid slowing the application down.

Riduidel
+2  A: 

B and C are eligable for garbage collection because you can't access them any more. With the unpredicatbility of the garbage collector all we know is they are quite likely to get collected at some point in the future.

Xian
+2  A: 

B has no reference to it so it will be garbage collected first, then it will understand that C has no reference to it, so C will be garbage collected. It is for illustration, Jvm is smart enough to scoop them in one sweep.

fastcodejava
There is no way to tell that B will be gc'ed first, i.e. you should not have finalizer code in B and C that depends on that order. The GC doesn't count the references, but checks for reachability. If both B and C cannot be reached, they are equally unreachable.
ammoQ
+2  A: 

As usual, this article is a must-read for whoever wants to understand what garbage collection does. It is well-written and has explanatory drawings.

Thomas Pornin
By opening the link, I need a username and password.
Bright010957
Ah, FTP strikes again. I have edited my post, with another link which should work better. There is also a PDF version.
Thomas Pornin
@Bright010957: Tada! https://ritdml.rit.edu/dspace/bitstream/1850/5112/1/PWilsonProceedings1992.pdf
Philip Potter