本人最近在学JVM,学习GC时,发现一道不错的题目,故此拿出来与大家一起分享。
public class ReliveObj {
public static ReliveObj obj;
@Override
protected void finalize() throws Throwable {
super.finalize();
obj = this;
}
@Override
public String toString() {
return "I am a ReliveObj";
}
public static void main(String[] args) throws InterruptedException {
obj = new ReliveObj();
System.out.println(obj);
obj = null;
System.out.println(obj);
System.gc();
TimeUnit.SECONDS.sleep(1);
System.out.println(obj);
obj = null;
System.out.println(obj);
System.gc();
TimeUnit.SECONDS.sleep(1);
System.out.println(obj);
}
}
问输出是什么,大家不妨试下,看看能不能做对,稍后公布答案