Skip to content

Instantly share code, notes, and snippets.

@GotoFinal
Created October 29, 2019 20:56
Show Gist options
  • Save GotoFinal/c0a4d0c7cfb335ea9401848a6470e816 to your computer and use it in GitHub Desktop.
Save GotoFinal/c0a4d0c7cfb335ea9401848a6470e816 to your computer and use it in GitHub Desktop.
class TestGC {
private static Runtime rt = Runtime.getRuntime();
private static long free() { return rt.maxMemory() - rt.totalMemory() + rt.freeMemory(); }
public static void main(String[] args) throws InterruptedException, IOException {
int blocks = 50;
int typeSize = 8;
long initiallyFree = free();
System.out.println("initially free: " + initiallyFree / 1000000);
ArrayList data = new ArrayList();
for (int n = 0; n < blocks; n++) {
data.add(new long[1024 * 1024-2]);
}
System.gc();
Thread.sleep(500);
long remainingFree = free();
System.out.println("remaining free: " + remainingFree / 1000000);
System.out.println("used: " + (initiallyFree - remainingFree) / 1000000);
System.out.println("expected usage: " + blocks * typeSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment