Reading runtime information about Java is useful sometime when your 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.
Hence sometimes it is beneficial to see what amount of memory is your application using and what is the free memory in system.
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 small example of getting/reading JVM Heap Size, Total Memory and used memory using Java Runtime api.
Following is the small example of getting/reading JVM Heap Size, Total Memory and used memory using Java Runtime api.
Core java Style:
System.gc();
Runtime runtime=Runtime.getRuntime();
System.out.println("total memory:"+runtime.totalMemory());
long usedMb=(runtime.totalMemory()-runtime.freeMemory())/1024/1024;
System.out.println("memory usage is:"+usedMb);
In Web Applications
System.gc();
Runtime runtime = Runtime.getRuntime();
log.info("total memory:"+runtime.totalMemory());
long usedMB = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
log.info("memory usage" + usedMB);
No comments:
Post a Comment