title: Garbage collection example in java?
body: <pre><code>class Beta { }
  
  class Alpha {
  static Beta b1  ;
  Beta b2;
  }
  
  public class Tester {
  
  public static void main(String[] args) {
  
      Beta b1 = new Beta();
      Beta b2 = new Beta();
      Alpha a1 = new Alpha();
      Alpha a2 = new Alpha();
  
      a1.b1 = b1 ;
      a1.b2 = b1 ;
      a2.b2 = b2 ;
  
      a1 = null ;
      b1 = null;
      b2 = null;
  
      System.out.println(" Line 1    " + " a1   "  + a1.b1);
      System.out.println(" Line 2    " + " a1   "  + a1.b2);
      System.out.println(" Line 3    " + " a2   " + a2.b2);
      System.out.println(" Line 4    " + " a1   " + a2.b1);
      System.out.println(" Line 5    " + " b1   " + b1);
      System.out.println(" Line 6    " + " b1   " + b2);
  
    }
  
   }  
  </code></pre>
  
  <p>I am not sure why only a1 object is available for garbage collection in the above program. I am expecting a1 , b1 and b2 to be collected by garbage collector.</p>
  
  <p>As you can see a1 , b1 and b2 were made NULL hence this is making objects to be available for garbage collection. If object is NULL or unreachable by any thread or reference variable it should be collected by garbage collector. </p>
  
  <p>Can someone help me understand the subtleness of the above program with good example and in more precise way ? Your help is appreciated.</p>