Methods for getting currently running JVM memory details

  The Runtime class have lot of useful function for getting information about JVM. In this example I am going get the memory details of currently running JVM. See the below example.

public class JvmMemoryDemo {
  public static void main(String args[]) {
    try{
System.out.println("Maximum amount of memory that JVM will attempt to use: " +  Runtime.getRuntime().maxMemory() + " bytes.");
System.out.println("Total amount of memory used by JVM: " +
Runtime.getRuntime().totalMemory() + " bytes.");
System.out.println("Total amount free memory available in JVM: " +
Runtime.getRuntime().freeMemory() + " bytes.");
    }
    catch(Exception e) {
e.printStackTrace();
    }
  }
}

  The Runtime.getRuntime().maxMemory() method return the maximum memory that JVM can able to use in bytes.
  The Runtime.getRuntime().totalMemory() method returns the memory that JVM currently used in bytes.
  The Runtime.getRuntime().freeMemory() method return the total amount memory available in JVM in bytes.

Note:
  You can able to use these methods for resolving memory related bugs present in your java application. For example you can able to calculate how much memory needed for running your application.

Comments

Popular Posts