I have two classes
Class A
{
//constructor
}
Class B
{
private A a;
public B()
{
a = new A();
}
}
Suppose I use an object of B [say b
] in my code and after I end up using it, I set it to null
. I know that the object of B is now available for garbage collection.
I know that after setting b to null, it will be immediately eligible for garbage collection? But what about the object of type A? Will it be available for garbage collection immediately after I set B to null
? Or will it be eligible for garbage collection after B is garbage collected?
Theoretically, until B is garbage collected, a
still has a reference to it? So will the SUN JVM compiler detect this immediately after setting b = null
;
The GC is smart enough to follow paths in graphs of objects in order to check if the existing references to some object are reachable. It can even detect cycles (like, in your case, if b
held a reference to a
and a
held a reference to b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With