Hi,
can anybody tell me why finalize is not called in this example?
I expect finalize to get called and write the file "final.txt".
I do see this file if I call finalize explicitly (see commented out line in main).
A Java VM will be started and stopped when I run this example, right?
Am I missing something?
Maarten
// File Main.java
package testgc;
public class Main {
public static void main(String[] args) throws Throwable {
if (true) {
Trash trash = new Trash();
// trash.finalize();
}
System.gc();
}
}
// File Trash.java (i.e. this code is not in file Main.java)
package testgc;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class Trash {
@Override
protected void finalize() throws Throwable {
PrintWriter out = new PrintWriter(new FileOutputStream("final.txt"));
out.println("Finalizing");
out.close();
}
}