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();

Sections in This Chapter

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

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