Class Runtime
public class Runtime extends Object
getRuntime().- See Also:
System
-
Method Summary
Modifier and Type Method Description voidaddShutdownHook(Thread hook)Registers a VM shutdown hook.intavailableProcessors()Returns the number of processor cores available to the VM, at least 1.Processexec(String prog)Executes the specified program in a separate native process.Processexec(String[] progArray)Executes the specified command and its arguments in a separate native process.Processexec(String[] progArray, String[] envp)Executes the specified command and its arguments in a separate native process.Processexec(String[] progArray, String[] envp, File directory)Executes the specified command and its arguments in a separate native process.Processexec(String prog, String[] envp)Executes the specified program in a separate native process.Processexec(String prog, String[] envp, File directory)Executes the specified program in a separate native process.voidexit(int code)Causes the VM to stop running and the program to exit.longfreeMemory()Returns the number of bytes currently available on the heap without expanding the heap.voidgc()Indicates to the VM that it would be a good time to run the garbage collector.InputStreamgetLocalizedInputStream(InputStream stream)Deprecated.OutputStreamgetLocalizedOutputStream(OutputStream stream)Deprecated.UseOutputStreamWriterinstead.static RuntimegetRuntime()Returns the singleRuntimeinstance for the current application.voidhalt(int code)Causes the VM to stop running, and the program to exit with the given return code.voidload(String pathName)Loads and links the dynamic library that is identified through the specified path.voidloadLibrary(String libName)Loads and links the library with the specified name.longmaxMemory()Returns the maximum number of bytes the heap can expand to.booleanremoveShutdownHook(Thread hook)Unregisters a previously registered VM shutdown hook.voidrunFinalization()Provides a hint to the VM that it would be useful to attempt to perform any outstanding object finalization.static voidrunFinalizersOnExit(boolean run)Deprecated.This method is unsafe.longtotalMemory()Returns the number of bytes taken by the heap at its current size.voidtraceInstructions(boolean enable)Switches the output of debug information for instructions on or off.voidtraceMethodCalls(boolean enable)Switches the output of debug information for methods on or off.
-
Method Details
-
exec
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 callingexec(progArray, null, null).- Parameters:
progArray- the array containing the program to execute as well as any arguments to the program.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exec
Executes the specified command and its arguments in a separate native process. The new process uses the environment provided inenvp. Calling this method is equivalent to callingexec(progArray, envp, null).- Parameters:
progArray- the array containing the program to execute as well as any arguments to the program.envp- the array containing the environment to start the new process in.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exec
Executes the specified command and its arguments in a separate native process. The new process uses the environment provided inenvpand the working directory specified bydirectory.- Parameters:
progArray- the array containing the program to execute as well as any arguments to the program.envp- the array containing the environment to start the new process in.directory- the directory in which to execute the program. Ifnull, execute if in the same directory as the parent process.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exec
Executes the specified program in a separate native process. The new process inherits the environment of the caller. Calling this method is equivalent to callingexec(prog, null, null).- Parameters:
prog- the name of the program to execute.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exec
Executes the specified program in a separate native process. The new process uses the environment provided inenvp. Calling this method is equivalent to callingexec(prog, envp, null).- Parameters:
prog- the name of the program to execute.envp- the array containing the environment to start the new process in.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exec
Executes the specified program in a separate native process. The new process uses the environment provided inenvpand the working directory specified bydirectory.- Parameters:
prog- the name of the program to execute.envp- the array containing the environment to start the new process in.directory- the directory in which to execute the program. Ifnull, execute if in the same directory as the parent process.- Returns:
- the new
Processobject that represents the native process. - Throws:
IOException- if the requested program can not be executed.
-
exit
public void exit(int code)Causes the VM to stop running and the program to exit. IfrunFinalizersOnExit(boolean)has been previously invoked with atrueargument, 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. -
gc
public 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. -
getRuntime
Returns the singleRuntimeinstance for the current application. -
load
Loads and links the dynamic library that is identified through the specified path. This method is similar toloadLibrary(String), but it accepts a full path specification whereasloadLibraryjust accepts the name of the library to load.- Parameters:
pathName- the absolute (platform dependent) path to the library to load.- Throws:
UnsatisfiedLinkError- if the library can not be loaded.
-
loadLibrary
Loads and links the library with the specified name. The mapping of the specified library name to the full path for loading the library is implementation-dependent.- Parameters:
libName- the name of the library to load.- Throws:
UnsatisfiedLinkError- if the library can not be loaded.
-
runFinalization
public void runFinalization()Provides a hint to the VM that it would be useful to attempt to perform any outstanding object finalization. -
runFinalizersOnExit
Deprecated.This method is unsafe.Sets the flag that indicates whether all objects are finalized when the VM 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-trueto enable finalization on exit,falseto disable it.
-
traceInstructions
public void traceInstructions(boolean enable)Switches the output of debug information for instructions on or off. On Android, this method does nothing. -
traceMethodCalls
public void traceMethodCalls(boolean enable)Switches the output of debug information for methods on or off. -
getLocalizedInputStream
Deprecated.UseInputStreamReaderinstead.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- the input stream to localize.- Returns:
- the localized input stream.
-
getLocalizedOutputStream
Deprecated.UseOutputStreamWriterinstead.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- the output stream to localize.- Returns:
- the localized output stream.
-
addShutdownHook
Registers a VM shutdown hook. A shutdown hook is aThreadthat is ready to run, but has not yet been started. All registered shutdown hooks will be executed when the VM terminates normally (typically when theexit(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 atrueargument, 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- 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.
-
removeShutdownHook
Unregisters a previously registered VM shutdown hook.- Parameters:
hook- the shutdown hook to remove.- Returns:
trueif the hook has been removed successfully;falseotherwise.- Throws:
IllegalStateException- if the VM is already shutting down.
-
halt
public 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. -
availableProcessors
public 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. -
freeMemory
public long freeMemory()Returns the number of bytes currently available on the heap without expanding the heap. SeetotalMemory()for the heap's current size. When these bytes are exhausted, the heap may expand. SeemaxMemory()for that limit. -
totalMemory
public 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. SeemaxMemory()for the maximum heap size, andfreeMemory()for an idea of how much the heap could currently contract. -
maxMemory
public long maxMemory()Returns the maximum number of bytes the heap can expand to. SeetotalMemory()for the current number of bytes taken by the heap, andfreeMemory()for the current number of those bytes actually used by live objects.
-
InputStreamReaderinstead.