How does Garbage Collection in Java work?
Asked Answered
B

9

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.

Bar answered 12/3, 2010 at 9:44 Comment(1)
I asked the other question in a different post: #2433761Bar
H
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.

Hagar answered 12/3, 2010 at 9:49 Comment(0)
T
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).

Ticktock answered 12/3, 2010 at 9:48 Comment(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.Ticktock
@Joachim: Are static variables not part of a class and not of an object?Bar
@Joachim: See topic start, I added an extra question.Bar
B
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

Barnet answered 12/3, 2010 at 9:48 Comment(0)
U
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.

Unfreeze answered 12/3, 2010 at 13:31 Comment(3)
By opening the link, I need a username and password.Bar
Ah, FTP strikes again. I have edited my post, with another link which should work better. There is also a PDF version.Unfreeze
@Bright010957: Tada! ritdml.rit.edu/dspace/bitstream/1850/5112/1/…Professoriate
A
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.

Altman answered 12/3, 2010 at 9:51 Comment(0)
E
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.

Electro answered 12/3, 2010 at 9:51 Comment(0)
D
1

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

Desiderata answered 12/3, 2010 at 9:48 Comment(0)
C
1

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

Caston answered 12/3, 2010 at 9:51 Comment(0)
S
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.

Scholasticate answered 12/3, 2010 at 9:56 Comment(1)
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.Adolpho

© 2022 - 2024 — McMap. All rights reserved.