Skip to content

Instantly share code, notes, and snippets.

@morris821028
Created April 24, 2018 11:58
Show Gist options
  • Save morris821028/235d9fa59f5fce4c4767e2b3c4075bc8 to your computer and use it in GitHub Desktop.
Save morris821028/235d9fa59f5fce4c4767e2b3c4075bc8 to your computer and use it in GitHub Desktop.
package bsh;
import org.junit.Test;
public class MemoryTest {
static public double printUsage() {
double cap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
cap /= 1048576;
System.out.printf("Usage %f MB\n", cap);
return cap;
}
static public void blockAlloc(int v) {
byte t[] = new byte[v];
System.out.printf("Alloc %d\n", t.length);
}
@Test
public void memory_gc() {
printUsage();
int v = 1024 * 1048576;
{
// Case 1:
// byte t[] = new byte[v];
// System.out.printf("Alloc %d\n", t.length);
// Case 2:
blockAlloc(v);
}
printUsage();
System.gc();
printUsage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment