java_geek
java_geek

Reputation: 18035

how to get the min and max heap size settings of a JVM from within a Java program

How to get the min and max heap size settings of a VM from within a Java program?

Upvotes: 11

Views: 3573

Answers (4)

metismo
metismo

Reputation: 457

You just need to use the Runtime object with Runtime.getRuntime() and then use methods like totalMemory() and freeMemory().

Upvotes: 4

instantsetsuna
instantsetsuna

Reputation: 9623

ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()

Upvotes: 1

cherouvim
cherouvim

Reputation: 31928

max heap size:

Runtime.getRuntime().maxMemory();

Some other calculations which you may find interesting:

Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long totalFreeMemory = freeMemory + (maxMemory - allocatedMemory);
long usedMemory = maxMemory - totalFreeMemory;

Upvotes: 14

Related Questions