Runtime class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Runtime class

Java Runtime class is used to interact with java runtime environment. Java Runtime class provides methods to execute a process invoke GC, get total and free memory etc. There is only one instance of java.lang.Runtime class is available for one java application.

The Runtime.getRuntime () method returns the singleton instance of Runtime class.

Important methods of Java Runtime class





public static Runtime getRuntime()

Returns the instance of Runtime class.


public void exit(int status)

Terminates the current virtual machine.


public void addShutdownHook(Thread hook)

Registers new hook thread.


public Process exec(String command)throws IOException

Executes given command in a separate process.


public int availableProcessors()

Returns no. of available processors.


public long freeMemory()

Returns amount of free memory in JVM.


public long totalMemory()

Returns amount of total memory in JVM.

Java Runtime exec () method

  1. public class Runtime1{  
  2.  public static void main(String args[])throws Exception{  
  3.   Runtime.getRuntime().exec("notepad");//will open a new notepad  
  4.  }  
  5. }  

How to shutdown system in Java

You can use shutdown -s command to shutdown system. For windows OS, you need to provide full path of shutdown command e.g. c:WindowsSystem32shutdown.

Here you can use -s switch to shutdown system, -r switch to restart system and -t switch to specify time delay.

  1. public class Runtime2{  
  2.  public static void main(String args[])throws Exception{  
  3.   Runtime.getRuntime().exec("shutdown -s -t 0");  
  4.  }  
  5. }  

How to shutdown windows system in Java

  1. public class Runtime2{  
  2.  public static void main(String args[])throws Exception{  
  3.   Runtime.getRuntime().exec("c:\Windows\System32\shutdown -s -t 0");  
  4.  }  
  5. }  

How to restart system in Java

  1. public class Runtime3{  
  2.  public static void main(String args[])throws Exception{  
  3.   Runtime.getRuntime().exec("shutdown -r -t 0");  
  4.  }  
  5. }  

Java Runtime available Processors ()

  1. public class Runtime4{  
  2.  public static void main(String args[])throws Exception{  
  3.   System.out.println(Runtime.getRuntime().availableProcessors());  
  4.  }  
  5. }  

Java Runtime free Memory () and total Memory () method

In the given program, after creating 10000 instances, free memory will be less than the previous free memory. But after gc() call, you will get more free memory.

  1. public class MemoryTest{  
  2.  public static void main(String args[])throws Exception{  
  3.   Runtime r=Runtime.getRuntime();  
  4.   System.out.println("Total Memory: "+r.totalMemory());  
  5.   System.out.println("Free Memory: "+r.freeMemory());  
  7.   for(int i=0;i<10000;i++){  
  8.    new MemoryTest();  
  9.   }  
  10.   System.out.println("After creating 10000 instance, Free Memory: "+r.freeMemory());  
  11.   System.gc();  
  12.   System.out.println("After gc(), Free Memory: "+r.freeMemory());  
  13.  }  
  14. }  
Total Memory: 100139008
Free Memory: 99474824
After creating 10000 instance, Free Memory: 99310552
After gc(), Free Memory: 100182832