Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Created December 31, 2018 00:30
Show Gist options
  • Save jaredsburrows/98aa969421137fe7394a451fbbd763c4 to your computer and use it in GitHub Desktop.
Save jaredsburrows/98aa969421137fe7394a451fbbd763c4 to your computer and use it in GitHub Desktop.
Playing around with finalize()
public class FinalTest {
private static int count = 0;
public static void main(String[] args) throws Exception {
System.gc();
System.out.println("Start");
for (int i = 0; i < 100_000; i++) {
new OtherClass(i);
}
System.out.println("End");
System.gc();
Thread.sleep(3000);
System.out.println("Cleaned " + count);
}
private static class OtherClass {
private int value;
public OtherClass(int value) {
this.value = value;
}
protected void finalize() throws Throwable {
super.finalize();
count++;
System.out.println("OtherClass " + value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment