Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Runtime

public class Runtime
extends Object

java.lang.Object
   ↳ java.lang.Runtime


Allows Java applications to interface with the environment in which they are running. Applications can not create an instance of this class, but they can get a singleton instance by invoking getRuntime().

See also:

Summary

Public methods

void addShutdownHook(Thread hook)

Registers a VM shutdown hook.

int availableProcessors()

Returns the number of processor cores available to the VM, at least 1.

Process exec(String[] progArray, String[] envp)

Executes the specified command and its arguments in a separate native process.

Process exec(String prog, String[] envp, File directory)

Executes the specified program in a separate native process.

Process exec(String[] progArray, String[] envp, File directory)

Executes the specified command and its arguments in a separate native process.

Process exec(String prog, String[] envp)

Executes the specified program in a separate native process.

Process exec(String prog)

Executes the specified program in a separate native process.

Process exec(String[] progArray)

Executes the specified command and its arguments in a separate native process.

void exit(int code)

Causes the VM to stop running and the program to exit.

long freeMemory()

Returns the number of bytes currently available on the heap without expanding the heap.

void gc()

Indicates to the VM that it would be a good time to run the garbage collector.

InputStream getLocalizedInputStream(InputStream stream)

This method was deprecated in API level 1. Use InputStreamReader instead.

OutputStream getLocalizedOutputStream(OutputStream stream)

This method was deprecated in API level 1. Use OutputStreamWriter instead.

static Runtime getRuntime()

Returns the single Runtime instance for the current application.

void halt(int code)

Causes the VM to stop running, and the program to exit with the given return code.

void load(String absolutePath)

Loads the shared library found at the given absolute path.

void loadLibrary(String nickname)

Loads a shared library.

long maxMemory()

Returns the maximum number of bytes the heap can expand to.

boolean removeShutdownHook(Thread hook)

Unregisters a previously registered VM shutdown hook.

void runFinalization()

Provides a hint to the runtime that it would be useful to attempt to perform any outstanding object finalization.

static void runFinalizersOnExit(boolean run)

This method was deprecated in API level 1. This method is unsafe.

long totalMemory()

Returns the number of bytes taken by the heap at its current size.

void traceInstructions(boolean enable)

Switches the output of debug information for instructions on or off.

void traceMethodCalls(boolean enable)

Switches the output of debug information for methods on or off.

Inherited methods

From class java.lang.Object

Public methods

addShutdownHook

Added in API level 1
void addShutdownHook (Thread hook)

Registers a VM shutdown hook. A shutdown hook is a Thread that is ready to run, but has not yet been started. All registered shutdown hooks will be executed when the VM terminates normally (typically when the exit(int) method is called).

Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that your code is run. Instead, you should use the most appropriate lifecycle notification (Activity.onPause, say).

Shutdown hooks are run concurrently and in an unspecified order. Hooks failing due to an unhandled exception are not a problem, but the stack trace might be printed to the console. Once initiated, the whole shutdown process can only be terminated by calling halt().

If runFinalizersOnExit(boolean) has been called with a true argument, garbage collection and finalization will take place after all hooks are either finished or have failed. Then the VM terminates.

It is recommended that shutdown hooks do not do any time-consuming activities, in order to not hold up the shutdown process longer than necessary.

Parameters
hook Thread: the shutdown hook to register.
Throws
IllegalArgumentException if the hook has already been started or if it has already been registered.
IllegalStateException if the VM is already shutting down.

availableProcessors

Added in API level 1
int availableProcessors ()

Returns the number of processor cores available to the VM, at least 1. Traditionally this returned the number currently online, but many mobile devices are able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly Bean) return the maximum number of cores that could be made available if there were no power or heat constraints.

Returns
int

exec

Added in API level 1
Process exec (String[] progArray, 
                String[] envp)

Executes the specified command and its arguments in a separate native process. The new process uses the environment provided in envp. Calling this method is equivalent to calling exec(progArray, envp, null).

Parameters
progArray String: the array containing the program to execute as well as any arguments to the program.
envp String: the array containing the environment to start the new process in.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exec

Added in API level 1
Process exec (String prog, 
                String[] envp, 
                File directory)

Executes the specified program in a separate native process. The new process uses the environment provided in envp and the working directory specified by directory.

Parameters
prog String: the name of the program to execute.
envp String: the array containing the environment to start the new process in.
directory File: the directory in which to execute the program. If null, execute if in the same directory as the parent process.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exec

Added in API level 1
Process exec (String[] progArray, 
                String[] envp, 
                File directory)

Executes the specified command and its arguments in a separate native process. The new process uses the environment provided in envp and the working directory specified by directory.

Parameters
progArray String: the array containing the program to execute as well as any arguments to the program.
envp String: the array containing the environment to start the new process in.
directory File: the directory in which to execute the program. If null, execute if in the same directory as the parent process.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exec

Added in API level 1
Process exec (String prog, 
                String[] envp)

Executes the specified program in a separate native process. The new process uses the environment provided in envp. Calling this method is equivalent to calling exec(prog, envp, null).

Parameters
prog String: the name of the program to execute.
envp String: the array containing the environment to start the new process in.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exec

Added in API level 1
Process exec (String prog)

Executes the specified program in a separate native process. The new process inherits the environment of the caller. Calling this method is equivalent to calling exec(prog, null, null).

Parameters
prog String: the name of the program to execute.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exec

Added in API level 1
Process exec (String[] progArray)

Executes the specified command and its arguments in a separate native process. The new process inherits the environment of the caller. Calling this method is equivalent to calling exec(progArray, null, null).

Parameters
progArray String: the array containing the program to execute as well as any arguments to the program.
Returns
Process the new Process object that represents the native process.
Throws
IOException if the requested program can not be executed.

exit

Added in API level 1
void exit (int code)

Causes the VM to stop running and the program to exit. If runFinalizersOnExit(boolean) has been previously invoked with a true argument, then all objects will be properly garbage-collected and finalized first. Use 0 to signal success to the calling process and 1 to signal failure. This method is unlikely to be useful to an Android application.

Parameters
code int

freeMemory

Added in API level 1
long freeMemory ()

Returns the number of bytes currently available on the heap without expanding the heap. See totalMemory() for the heap's current size. When these bytes are exhausted, the heap may expand. See maxMemory() for that limit.

Returns
long

gc

Added in API level 1
void gc ()

Indicates to the VM that it would be a good time to run the garbage collector. Note that this is a hint only. There is no guarantee that the garbage collector will actually be run.

getLocalizedInputStream

Added in API level 1
InputStream getLocalizedInputStream (InputStream stream)

This method was deprecated in API level 1.
Use InputStreamReader instead.

Returns the localized version of the specified input stream. The input stream that is returned automatically converts all characters from the local character set to Unicode after reading them from the underlying stream.

Parameters
stream InputStream: the input stream to localize.
Returns
InputStream the localized input stream.

getLocalizedOutputStream

Added in API level 1
OutputStream getLocalizedOutputStream (OutputStream stream)

This method was deprecated in API level 1.
Use OutputStreamWriter instead.

Returns the localized version of the specified output stream. The output stream that is returned automatically converts all characters from Unicode to the local character set before writing them to the underlying stream.

Parameters
stream OutputStream: the output stream to localize.
Returns
OutputStream the localized output stream.

getRuntime

Added in API level 1
Runtime getRuntime ()

Returns the single Runtime instance for the current application.

Returns
Runtime

halt

Added in API level 1
void halt (int code)

Causes the VM to stop running, and the program to exit with the given return code. Use 0 to signal success to the calling process and 1 to signal failure. Neither shutdown hooks nor finalizers are run before exiting. This method is unlikely to be useful to an Android application.

Parameters
code int

load

Added in API level 1
void load (String absolutePath)

Loads the shared library found at the given absolute path. This should be of the form /path/to/library/libMyLibrary.so. Most callers should use loadLibrary(String) instead, and let the system find the correct file to load.

Parameters
absolutePath String
Throws
UnsatisfiedLinkError if the library can not be loaded, either because it's not found or because there is something wrong with it.

loadLibrary

Added in API level 1
void loadLibrary (String nickname)

Loads a shared library. Class loaders have some influence over this process, but for a typical Android app, it works as follows:

Given the name "MyLibrary", that string will be passed to mapLibraryName(String). That means it would be a mistake for the caller to include the usual "lib" prefix and ".so" suffix.

That file will then be searched for on the application's native library search path. This consists of the application's own native library directory followed by the system's native library directories.

Parameters
nickname String
Throws
UnsatisfiedLinkError if the library can not be loaded, either because it's not found or because there is something wrong with it.

maxMemory

Added in API level 1
long maxMemory ()

Returns the maximum number of bytes the heap can expand to. See totalMemory() for the current number of bytes taken by the heap, and freeMemory() for the current number of those bytes actually used by live objects.

Returns
long

removeShutdownHook

Added in API level 1
boolean removeShutdownHook (Thread hook)

Unregisters a previously registered VM shutdown hook.

Parameters
hook Thread: the shutdown hook to remove.
Returns
boolean true if the hook has been removed successfully; false otherwise.
Throws
IllegalStateException if the VM is already shutting down.

runFinalization

Added in API level 1
void runFinalization ()

Provides a hint to the runtime that it would be useful to attempt to perform any outstanding object finalization.

runFinalizersOnExit

Added in API level 1
void runFinalizersOnExit (boolean run)

This method was deprecated in API level 1.
This method is unsafe.

Sets the flag that indicates whether all objects are finalized when the runtime is about to exit. Note that all finalization which occurs when the system is exiting is performed after all running threads have been terminated.

Parameters
run boolean: true to enable finalization on exit, false to disable it.

totalMemory

Added in API level 1
long totalMemory ()

Returns the number of bytes taken by the heap at its current size. The heap may expand or contract over time, as the number of live objects increases or decreases. See maxMemory() for the maximum heap size, and freeMemory() for an idea of how much the heap could currently contract.

Returns
long

traceInstructions

Added in API level 1
void traceInstructions (boolean enable)

Switches the output of debug information for instructions on or off. On Android, this method does nothing.

Parameters
enable boolean

traceMethodCalls

Added in API level 1
void traceMethodCalls (boolean enable)

Switches the output of debug information for methods on or off.

Parameters
enable boolean
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.