11

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

trincot
  • 317,000
  • 35
  • 244
  • 286
java_geek
  • 17,585
  • 30
  • 91
  • 113

4 Answers4

14

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;
cherouvim
  • 31,725
  • 15
  • 104
  • 153
4

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

fospathi
  • 537
  • 1
  • 6
  • 7
metismo
  • 457
  • 2
  • 6
1
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()
instantsetsuna
  • 9,183
  • 8
  • 25
  • 28