0
class Node<E> {
E element;
Node<E> left;
Node<E> right;
...

Node<E> root;
//...
Node<E> a;
Node<E> b; // = a.left
Node<E> c; // = b.left

Node a the one which is in connection with the root of the tree.

I would like to remove b from root->...->a->b->c chain to get root->...a->c

My question:

If i set a.left reference to c, will b get garbage collected, or i must also set b.left reference to null?

I know an object is collected if no reference to it, but what if there is reference from it? I suppose it does not count, but i would like to be sure about it.

freestar
  • 418
  • 3
  • 18
  • 1
    You are correct. An object is eligible for collection if there is no reference to it. Period. – user207421 Feb 20 '16 at 09:47
  • You could see this: http://stackoverflow.com/questions/3506303/question-about-garbage-collection-in-java?rq=1. The first anwer by Jon Skeet states "garbage collects everything that it hasn't deemed to be live.". – Jose Luis Feb 20 '16 at 09:48
  • You can always build a small example using [`PhantimReference`s](https://docs.oracle.com/javase/8/docs/api/java/lang/ref/PhantomReference.html) to see that the reference to `b` is really garbage-collected. – Turing85 Feb 20 '16 at 11:30

0 Answers0