This Java Program will compute the maximum Heap memory, available Heap Memory and Used Memory statistics.
public class HeapStatMemory {
public static void main(String[] args) {
int mb = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
System.out
.println("**********Heap Memory statistics [MB]**************");
// Compute and print used memory
System.out.println("Used Memory:"
+ (runtime.totalMemory() - runtime.freeMemory()) / mb);
// Compute and Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);
// Compute and Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);
// Compute and Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
System.out
.println("***************************************************");
}
}