Java Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 6.00

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 Runtim 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();
   rt.gc();

The following code is correct:

   Runtime rt = Runtime.getRuntime();
   rt.gc();

Table of Contents

 About This Book

 Installing JDK 1.4 on Windows 2000

 Installing JDK 1.5 on Windows XP

 Installing JDK 1.6 on Windows XP

 Execution Process, Entry Point, Input and Output

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 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

 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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Runtime.getRuntime() - Getting the Runtime Object