Skip to content

Instantly share code, notes, and snippets.

@jerolba
Created January 22, 2023 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerolba/9cc8989867b15bb31c033aa30bc4847f to your computer and use it in GitHub Desktop.
Save jerolba/9cc8989867b15bb31c033aa30bc4847f to your computer and use it in GitHub Desktop.
Maximum number of objects simultaneously allocated: 10000000
Max memory: 16MB
Max used memory: 11MB
Max free memory: 11MB
Total Time: 9 secconds
public class Test {
static int objectCount = 0;
public static int getObjectCount() {
return objectCount;
}
public Test() {
objectCount++;
}
@Override
public void finalize() {
// objectCount--;
}
public static void main(String[] args) {
int maxObjects = 0;
long maxMemory = 0;
long maxUsedMemory = 0;
long maxFreeMemory = 0;
long memory = 0;
long usedMemory = 0;
long freeMemory = 0;
final long t0 = System.currentTimeMillis();
Test test = null;
for (int i = 0; i < 10000000; i++) {
// System.gc();
test = new Test();
memory = Runtime.getRuntime().totalMemory();
freeMemory = Runtime.getRuntime().freeMemory();
usedMemory = memory - freeMemory;
if (maxMemory < memory) {
maxMemory = memory;
}
if (maxFreeMemory < freeMemory) {
maxFreeMemory = freeMemory;
}
if (maxUsedMemory < usedMemory) {
maxUsedMemory = usedMemory;
}
if (maxObjects < getObjectCount()) {
maxObjects = getObjectCount();
}
}
final long t1 = System.currentTimeMillis();
System.out.println(
"Maximum number of objects simultaneously allocated: " + maxObjects);
System.out.println("Max memory: " + maxMemory / 1024 / 1024 + "MB");
System.out.println("Max used memory: " + maxUsedMemory / 1024 / 1024 + "MB");
System.out.println("Max free memory: " + maxFreeMemory / 1024 / 1024 + "MB");
System.out.println("Total Time: " + (t1 - t0) / 60 + " secconds");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment