Showing posts with label System.getRuntime(). Show all posts
Showing posts with label System.getRuntime(). Show all posts

Saturday 18 April 2015

Getting JVM heap size, used memory, total memory using Java Runtime

Getting JVM heap size, used memory, total memory using Java Runtime.

Reading runtime java virtual memory usage is useful when the system/application is struggling in getting resources. System Memory is one of the main resource that an application developer has to consider while managing the application.

Java’s Runtime class provide lot of information about the resource details of Java Virtual Machine or JVM. The memory consumed by the JVM can be read by different methods in Runtime class. Following is the  example of reading JVM Heap Size, Total Memory and used memory using Java Runtime api.

 public class MemoryUtilization {  
   public static void main(String [] args) {  
     int conversion = 1024*1024;       
     Runtime runtime = Runtime.getRuntime();       
     System.out.println("##### Heap utilization #####");       
     System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / conversion);  
     System.out.println("Free Memory:" + runtime.freeMemory() / conversion);  
     System.out.println("Total Memory:" + runtime.totalMemory() / conversion);       
     System.out.println("Max Memory:" + runtime.maxMemory() /conversion);  
   }  
 }