Created
December 31, 2018 00:30
-
-
Save jaredsburrows/98aa969421137fe7394a451fbbd763c4 to your computer and use it in GitHub Desktop.
Playing around with finalize()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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