A
asit
Just look into the code snippet
class A{
static int ct;
A aob;
String name;
public A() {
ct++;
name = ct + "";
System.out.println("cretaed : " + name);
}
protected void finalize() {
System.out.println("freed : " + name);
}
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
c=b;
c.aob=null;
System.gc();
}
}
The question was when the control reaches System.gc() line, how many
objects should be eligible for CG ??
My analysis shows it should be 1, but why the output indicates 2
objects ???
class A{
static int ct;
A aob;
String name;
public A() {
ct++;
name = ct + "";
System.out.println("cretaed : " + name);
}
protected void finalize() {
System.out.println("freed : " + name);
}
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
c=b;
c.aob=null;
System.gc();
}
}
The question was when the control reaches System.gc() line, how many
objects should be eligible for CG ??
My analysis shows it should be 1, but why the output indicates 2
objects ???