Top Description Inners Fields Constructors Methods
java.lang

public Class Runtime

extends Object
Class Inheritance
Imports
java.io.*, java.math.BigInteger, java.util.regex.Matcher, .Pattern, java.util.stream.Collectors, java.util.List, .Optional, .StringTokenizer, jdk.internal.access.SharedSecrets, jdk.internal.javac.Restricted, jdk.internal.reflect.CallerSensitive, .Reflection

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

An application cannot create its own instance of this class.

Shutdown Sequence

The Java Virtual Machine initiates the shutdown sequence in response to one of several events:

  1. when the number of live non-daemon threads drops to zero for the first time (see note below on the JNI Invocation API);
  2. when the Runtime.exit or System.exit method is called for the first time; or
  3. when some external event occurs, such as an interrupt or a signal is received from the operating system.

At the beginning of the shutdown sequence, the registered shutdown hooks are started in some unspecified order. They run concurrently with any daemon or non-daemon threads that were alive at the beginning of the shutdown sequence.

After the shutdown sequence has begun, registration and de-registration of shutdown hooks with addShutdownHook and removeShutdownHook is prohibited. However, creating and starting new threads is permitted. New threads run concurrently with the registered shutdown hooks and with any daemon or non-daemon threads that are already running.

The shutdown sequence finishes when all shutdown hooks have terminated. At this point, the Java Virtual Machine terminates as described below.

It is possible that one or more shutdown hooks do not terminate, for example, because of an infinite loop. In this case, the shutdown sequence will never finish. Other threads and shutdown hooks continue to run and can terminate the JVM via the halt method.

Prior to the beginning of the shutdown sequence, it is possible for a program to start a shutdown hook by calling its start method explicitly. If this occurs, the behavior of the shutdown sequence is unspecified.

Java Virtual Machine Termination

The JVM terminates when the shutdown sequence finishes or when halt is called. In contrast to exit, the halt method does not initiate the shutdown sequence.

When the JVM terminates, all threads are immediately prevented from executing any further Java code. This includes shutdown hooks as well as daemon and non-daemon threads. This means, for example, that:

Implementation Note

Native code typically uses the JNI Invocation API to control launching and termination of the JVM. Such native code invokes the JNI_CreateJavaVM function to launch the JVM. Subsequently, the native code invokes the DestroyJavaVM function to await termination of that JVM. The DestroyJavaVM function is responsible for initiating the shutdown sequence when the number of live non-daemon threads first drops to zero. When the shutdown sequence completes and the JVM terminates, control is returned to the native code that invoked DestroyJavaVM. This behavior differs from the exit or halt methods. These methods typically terminate the OS process hosting the JVM and do not interact with the JNI Invocation API.

Since
1.0
Java Language Specification
12.8 Program Exit
External Specification
Java Native Interface Specification
See Also
java.lang.Runtime#getRuntime()

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Runtime.Version

A representation of a version string for an implementation of the Java SE Platform.

private static class

Field Summary

Modifier and TypeField and Description
private static final Runtime
private static Runtime.Version

Constructor Summary

AccessConstructor and Description
private
Runtime()

Don't let anyone else instantiate this class

Method Summary

Modifier and TypeMethod and Description
public void
addShutdownHook(Thread
An initialized but unstarted Thread object
hook
)

Registers a new virtual-machine shutdown hook.

public native int

Returns:

the maximum number of processors available to the virtual machine; never smaller than one
availableProcessors
()

Returns the number of processors available to the Java virtual machine.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String
a specified system command.
command
)
Deprecated since 18. This method is error-prone and should not be used, the corresponding method exec(String[]) or ProcessBuilder should be used instead.

Executes the specified string command in a separate process.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String
a specified system command.
command
,
String[]
array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
envp
)
Deprecated since 18. This method is error-prone and should not be used, the corresponding method exec(String[], String[]) or ProcessBuilder should be used instead.

Executes the specified string command in a separate process with the specified environment.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String
a specified system command.
command
,
String[]
array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
envp
,
File
the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
dir
)
Deprecated since 18. This method is error-prone and should not be used, the corresponding method exec(String[], String[], File) or ProcessBuilder should be used instead.

Executes the specified string command in a separate process with the specified environment and working directory.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String[]
array containing the command to call and its arguments.
cmdarray
)

Executes the specified command and arguments in a separate process.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String[]
array containing the command to call and its arguments.
cmdarray
,
String[]
array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
envp
)

Executes the specified command and arguments in a separate process with the specified environment.

public Process

Returns:

A new Process object for managing the subprocess
exec
(String[]
array containing the command to call and its arguments.
cmdarray
,
String[]
array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
envp
,
File
the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
dir
)

Executes the specified command and arguments in a separate process with the specified environment and working directory.

public void
exit(int
Termination status. By convention, a nonzero status code indicates abnormal termination.
status
)

Initiates the shutdown sequence of the Java Virtual Machine.

public native long

Returns:

an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.
freeMemory
()

Returns the amount of free memory in the Java Virtual Machine.

public native void
gc()

Runs the garbage collector in the Java Virtual Machine.

public static Runtime

Returns:

the Runtime object associated with the current Java application.
getRuntime
()

Returns the runtime object associated with the current Java application.

public void
halt(int
Termination status. By convention, a nonzero status code indicates abnormal termination. If the exit (equivalently, System.exit) method has already been invoked then this status code will override the status code passed to that method.
status
)

Immediately terminates the Java Virtual Machine.

public void
load(String
the file to load.
filename
)

Loads the native library specified by the filename argument.

pack-priv void
load0(Class<?> fromClass, String filename)

public void
loadLibrary(String
the name of the library.
libname
)

Loads the native library specified by the libname argument.

pack-priv void
loadLibrary0(Class<?> fromClass, String libname)

public native long

Returns:

the maximum amount of memory that the virtual machine will attempt to use, measured in bytes
maxMemory
()

Returns the maximum amount of memory that the Java virtual machine will attempt to use.

public boolean

Returns:

true if the specified hook had previously been registered and was successfully de-registered, false otherwise.
removeShutdownHook
(Thread
the hook to remove
hook
)

De-registers a previously-registered virtual-machine shutdown hook.

public void
runFinalization()
Deprecated for removal since 18. Finalization has been deprecated for removal.

Runs the finalization methods of any objects pending finalization.

public native long

Returns:

the total amount of memory currently available for current and future objects, measured in bytes.
totalMemory
()

Returns the total amount of memory in the Java virtual machine.

public static Runtime.Version

Returns:

the Version of the Java Runtime Environment
version
()

Returns the version of the Java Runtime Environment as a Version.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait