class MyObj {
public void print() {
System.out.println("Hello!");
}
@Override
protected void finalize() throws Throwable {
System.out.println("MyObj Destroyed");
super.finalize();
}
public static void main(String... args) {
MyObj obj = new MyObj();
obj.print();
for (int i = 0; i < 10000000; i++) {
byte[] b = new byte[100];
}
System.out.println("done");
}
}
Hello!
MyObj Destroyed
done
but when I change the main method to this
public static void main(String... args) {
MyObj obj = new MyObj();
obj.print();
for (int i = 0; i < 10000000; i++) {
byte[] b = new byte[100]; //just create objects to force garbage collector to run
}
obj.print(); //call print here
System.out.println("done");
}
Hello!
Hello!
done
Does Java automatically set strong references to null on their last use?