Java Runtime Class: Interacting with the Java Runtime Environment
Explore the Java Runtime class, a powerful utility for interacting with the Java runtime environment. Learn how to execute processes, manage memory, invoke the garbage collector, and more using the singleton instance provided by Runtime.getRuntime()
.
Java Runtime Class
The Java Runtime class is used to interact with the Java runtime environment. It provides methods to execute a process, invoke the garbage collector, get total and free memory, etc. There is only one instance of the java.lang.Runtime
class available for one Java application.
The Runtime.getRuntime()
method returns the singleton instance of the Runtime class.
Important Methods of Java Runtime Class
No. | Method | Description |
---|---|---|
1. | public static Runtime getRuntime() |
Returns the instance of the Runtime class. |
2. | public void exit(int status) |
Terminates the current virtual machine. |
3. | public void addShutdownHook(Thread hook) |
Registers a new hook thread. |
4. | public Process exec(String command) throws IOException |
Executes the given command in a separate process. |
5. | public int availableProcessors() |
Returns the number of available processors. |
6. | public long freeMemory() |
Returns the amount of free memory in JVM. |
7. | public long totalMemory() |
Returns the amount of total memory in JVM. |
Java Runtime exec()
Method
FileName: Runtime1.java
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
Use the shutdown -s
command to shutdown the system. For Windows OS, provide the full path of the shutdown command, e.g., c:\\Windows\\System32\\shutdown
. Use the -s
switch to shutdown the system, -r
switch to restart the system, and -t
switch to specify a time delay.
Shutdown Example
FileName: Runtime2.java
public class Runtime2 {
public static void main(String args[]) throws Exception {
Runtime.getRuntime().exec("shutdown -s -t 0");
}
}
Windows Shutdown Example
FileName: Runtime2.java
public class Runtime2 {
public static void main(String args[]) throws Exception {
Runtime.getRuntime().exec("c:\\Windows\\System32\\shutdown -s -t 0");
}
}
Restart Example
FileName: Runtime3.java
public class Runtime3 {
public static void main(String args[]) throws Exception {
Runtime.getRuntime().exec("shutdown -r -t 0");
}
}
Java Runtime availableProcessors()
Method
FileName: Runtime4.java
public class Runtime4 {
public static void main(String args[]) throws Exception {
System.out.println(Runtime.getRuntime().availableProcessors());
}
}
Java Runtime freeMemory()
and totalMemory()
Methods
In the given program, after creating 10,000 instances, free memory will be less than the previous free memory. But after calling gc()
, you will get more free memory.
FileName: MemoryTest.java
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 instances, Free Memory: " + r.freeMemory());
System.gc();
System.out.println("After gc(), Free Memory: " + r.freeMemory());
}
}
Output
Total Memory: 100139008
Free Memory: 99474824
After creating 10000 instances, Free Memory: 99310552
After gc(), Free Memory: 100182832