0

How to get total and used heap memory of a process in java?

    strong textint mb = 1024 * 1024;
    Runtime instance = Runtime.getRuntime();
    System.out.println("***** Heap utilization statistics [MB] *****\n");
    System.out.println("Total Memory: " + instance.totalMemory() / mb);
    String str = "9280";
    Process pr = instance.exec("cmd /c jmap -heap "+str);

This is Windows based, I need it for cross platform.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • you can put the last line in the if loop under condition `System.getProperty("os.name").startsWith("Windows")` – Palcente May 12 '16 at 13:04
  • again i need to do the same for Linux again for Solaris . can there be a generic solution which i can use for all the platform. – Saswat Kumar Sahoo May 12 '16 at 13:10
  • You want to trigger a "heap dump"? You may call the HotSpot VM Bean and trigger the heap dump method. Or the "MemoryMXBean" class for information only. – Konrad May 12 '16 at 13:24
  • using "MemoryMXBean" we can calculate JVM heap memory but i want to calculate the total and used memory of a current running process assuming i have Process id. – Saswat Kumar Sahoo May 12 '16 at 13:31

0 Answers0