0

I have a question related to Garbage Collection in Java

Let us thought of a situation

There are three classes Class A,Class B,Class C

  • Now A has B
  • B has C

Now say A=null; (I assign null to A)

Will A be eligible for Garbage Collection ?

If yes, What will happen to C then ?

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
  • It would help if you'd show us actual code rather than just describing it... If you're asking whether it handled cyclical references, the answer is "yes" . – Jon Skeet Aug 13 '14 at 11:38
  • What is a 'query related garbage collector'? – isnot2bad Aug 13 '14 at 11:40
  • @isnot2bad I would imagine a 'question related to garbage collection'. – Djon Aug 13 '14 at 11:45
  • possible duplicate of [Garbage Collection in Java and Circular References](http://stackoverflow.com/questions/1910194/garbage-collection-in-java-and-circular-references) – Raedwald Aug 13 '14 at 11:49
  • I would have shown the code, but I have just assumed a situation. And considering the duplication the question is slight difference. Classes A,B and C can be of different type. In http://stackoverflow.com/questions/25285095/java-garbage-collector-class-a-class-b-class-c-class-b-and-cyclic-refere Node has a same type of object inside it. – Hardik Trivedi Aug 13 '14 at 11:55

2 Answers2

4

The short answer is yes.

The garbage collector searches the object graph and can identify when object sub-trees have been disconnected and sweep the entire sub-tree; regardless of the fact a cycle exists in the subtree.

If the instance of the C class gets collected; the GC will execute the finalizer and then destroy the object.

pfranza
  • 3,292
  • 2
  • 21
  • 34
0

It is not about what is within which object. The GC will just swipe through all objects and check if it is reachable. finalize() method will be executed only when the GC determines that an Object is unreachable.

public class TryThreads {
B b = new B();

@Override
protected void finalize() throws Throwable {
    System.out.println("TryThreads finalizing..");
}

public static void main(String[] args) {
    TryThreads t = new TryThreads();
    t = null;
    System.gc();
    System.runFinalization();

}

}

class B {
String s = new String("hello");

@Override
protected void finalize() throws Throwable {
    System.out.println("B finalizing");
}

}

O/P :

B finalizing
TryThreads finalizing..

Now, if b of the TryThread instance t had a Strong reference.

public class TryThreads { B b = new B();

@Override
protected void finalize() throws Throwable {
    System.out.println("TryThreads finalizing..");
}

public static void main(String[] args) {
    TryThreads t = new TryThreads();
    B b = t.b; // keeping a reference of b within t. only t will be collected, not b.
    t = null;
    System.gc();
    System.runFinalization();

}

}

class B {
String s = new String("hello");

@Override
protected void finalize() throws Throwable {
    System.out.println("B finalizing");
}

}

O/P :

TryThreads finalizing..
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 1
    I'm not the downvoter, but that's a very complex answer to the simple question. And I'm not even sure you really answered the questions, which are: "Will A be eligible for garbage collection?" and "What happens to C?" – isnot2bad Aug 13 '14 at 12:26
  • @isnot2bad - thanks for telling what you felt was wrong with my answer.. I've indeed answered the OP's question. In the second case I've also shown what happens if a reference to the inner variable leaks. :( – TheLostMind Aug 13 '14 at 12:30