Runtime.getRuntime() - Getting the Runtime Object
This section describes the system Runtime object and the Runtime.getRuntime() method to get it from the JVM.
When a Java application program is running inside a JVM, the JVM offers an instance of the
Runtime class to the program. The Java program can use this Runtime instance
to interact with the JVM and the supporting operating system.
There are several interesting types of methods available on the Runtime instance:
- Getting dynamic information about the JVM, like freeMemory().
- Launching a separate process at the operating system level, like exec().
- Interacting with the shutdown sequence of the JVM, like addShutdownHook().
- Forcing an immediate garbage collection job on the JVM, like gc().
The Runtime class is a special class. It represents the run time environment of
the Java application program running inside the JVM.
You can not create a new instance of Runtime class in your application program.
You can only get the instance created by the JVM for your application program by
calling the Runtime.getRuntime() method.
So, the following code is wrong:
Runtime rt = new Runtime();
The following code is correct:
Runtime rt = Runtime.getRuntime();
Last update: 2014.
Table of Contents
About This Book
Installing JDK 1.8 on Windows
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
►System Properties and Runtime Object Methods
JVM and OS System Properties
System.setProperty() - Setting Your Own Properties
►Runtime.getRuntime() - Getting the Runtime Object
freeMemory() - Getting JVM Free Memory Information
Calculating Memory Usage of an Array
exec() - Executing Operating System Commands
Generic Classes and Parameterized Types
Generic Methods and Type Inference
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs
Garbage Collection and the gc() Method
PDF Printing Version