11

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.

JordyOnrust
  • 652
  • 1
  • 6
  • 16
  • I asked the other question in a different post: http://stackoverflow.com/questions/2433261/how-does-garbage-collection-in-java-work-with-classes – JordyOnrust Mar 12 '10 at 14:17

9 Answers9

9

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.

enocom
  • 1,496
  • 1
  • 15
  • 22
bruno conde
  • 47,767
  • 15
  • 98
  • 117
6

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
  • 302,674
  • 57
  • 556
  • 614
  • 3
    @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 Mar 12 '10 at 10:03
  • @Joachim: Are static variables not part of a class and not of an object? – JordyOnrust Mar 12 '10 at 11:47
  • @Joachim: See topic start, I added an extra question. – JordyOnrust Mar 12 '10 at 13:27
4

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

Mahmoud Hanafy
  • 7,958
  • 12
  • 47
  • 62
4

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
  • 72,986
  • 14
  • 147
  • 189
2

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
  • 22,052
  • 14
  • 85
  • 185
2

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 Stannard
  • 376
  • 3
  • 13
1

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

extraneon
  • 23,575
  • 2
  • 47
  • 51
1

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

Artsiom Anisimau
  • 1,139
  • 3
  • 11
  • 26
0

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
  • 39,895
  • 28
  • 133
  • 186
  • 2
    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. – Erich Kitzmueller Mar 12 '10 at 13:57