public class Main {
public static void main(String[] args) throws InterruptedException {
ClassA a = new ClassA();
a = null;
//Runtime.getRuntime().gc();
Thread.sleep(4000);
}
}
public class ClassA {
@Override
public void finalize(){
System.out.println("cleaned");
}
}
With the above code finalize() never executes. Nothing is printed to the console. When Removing comment from gc(), finalize() executes, and "cleaned" is printed to the console. Why do I have to call to the garbage collector explicitly ?