Monthly Archives: February 2016

Runtime class in Java

Runtime is a singleton class in Java. You can perform below tasks using Runtime class.

  1. Execute any OS command – exec()
  2. Find free memory – freeMemory()
  3. Find total memory – totalMemory()
  4. Terminate the Java application – exit()
  5. Get the count of processors available to JVM – availableProcessors()

Below example demonstrates Runtime usage.

package runtime;

 * Created by Sagar on 16-04-2016.
public class RuntimeTest {

    public static void main(String [] args)throws Exception{
        Runtime runtime=Runtime.getRuntime();
        System.out.println("Count of Processors -> "+runtime.availableProcessors());
        System.out.println("Total Memory -> "+runtime.totalMemory());
        System.out.println("Free Memory -> "+runtime.freeMemory());
        //Launch calculator using exec. You can execute any command using exec
        Process calculatorProcess = runtime.exec("calc.exe");
        //Program will continue running until you close the calculator


Here is the sample output of above code.
Count of Processors -> 4
Total Memory -> 56623104
Free Memory -> 54190136