Sunday, April 27, 2014

Get number of memory in Java VM

Every Java application has a single instance of class java.lang.Runtime that allows the application to interface with the environment in which the application is running.
  • long freeMemory()
    Returns the amount of free memory in the Java Virtual Machine.
  • long maxMemory()
    Returns the maximum amount of memory that the Java virtual machine will attempt to use.
  • long totalMemory()
    Returns the total amount of memory in the Java virtual machine.
public class testMem {
    
    public static void main(String[] args){
        System.out.println("helloraspberrypi.blogspot.com");
        
        //the total amount of memory in the Java virtual machine
        long totalMem = Runtime.getRuntime().totalMemory();
        //the maximum amount of memory that the Java virtual machine will attempt to use
        long maxMem = Runtime.getRuntime().maxMemory();
        //the amount of free memory in the Java Virtual Machine
        long freeMem = Runtime.getRuntime().freeMemory();
        
        System.out.println("totalMemory() - " + totalMem);
        System.out.println("maxMemory() - " + maxMem);
        System.out.println("freeMemory() - " + freeMem);
        
    }
    
}

We can run java with -Xms and -Xmx options to set Java heap size:
  • -Xms<size> set initial Java heap size
  • -Xmx<size> set maximum Java heap size

Run on Raspberry Pi:

No comments: