Runtime Class

The Java Runtime class facilitates communication with the Java runtime environment. It offers functionalities such as process execution, garbage collection invocation, and memory retrieval. Each Java application has a single instance of the java.lang.Runtime class accessible for use.

The method Runtime.getRuntime provides access to the sole instance of the Runtime class.

Important methods of Java Runtime class

No. Method Description
1) public static Runtime getRuntime() returns the instance of Runtime class.
2) public void exit(int status) terminates the current virtual machine.
3) public void addShutdownHook(Thread hook) registers new hook thread.
4) public Process exec(String command)throws IOException executes given command in a separate process.
5) public int availableProcessors() returns no. of available processors.
6) public long freeMemory() returns amount of free memory in JVM.
7) public long totalMemory() returns amount of total memory in JVM.

Java Runtime exec method

Example

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

How to shutdown system in Java

The system can be shut down using the "shutdown -s" command. In Windows operating systems, it is necessary to specify the complete path of the shutdown command, for example, c:\\Windows\\System32\\shutdown.

You have the option to utilize the -s switch for system shutdown, the -r switch for system restart, and the -t switch to indicate a time delay.

Example

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

How to shutdown windows system in Java

Example

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

How to restart system in Java

Example

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

Java Runtime availableProcessors

Example

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

Java Runtime freeMemory and totalMemory method

In the provided program, once 10,000 instances are generated, the available free memory will decrease compared to the previous amount of free memory. However, upon invoking the gc function, a greater amount of free memory will be reclaimed.

Example

public class MemoryTest{
 public static void main(String args[])throws Exception{
  Runtime r=Runtime.getRuntime();
  System.out.println("Total Memory: "+r.totalMemory());
  System.out.println("Free Memory: "+r.freeMemory());
  
  for(int i=0;i<10000;i++){
   new MemoryTest();
  }
  System.out.println("After creating 10000 instance, Free Memory: "+r.freeMemory());
  System.gc();
  System.out.println("After gc(), Free Memory: "+r.freeMemory());
 }
}
Example

Total Memory: 100139008
Free Memory: 99474824
After creating 10000 instance, Free Memory: 99310552
After gc(), Free Memory: 100182832

Input Required

This code uses input(). Please provide values below: