|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Runtime
public class Runtime
Runtime provides control over the runtime system, i.e., memory management, VM termination cleanup, execution of external commands, etc.
Constructor Summary | |
---|---|
Runtime()
|
Method Summary | |
---|---|
void |
addShutdownHook(Thread hook)
add a shutdown hook adds a shutdown hook that will be started on a call to exit(int). |
int |
availableProcessors()
Returns the number of processors available to the Java Virtual machine. |
Process |
exec(String command)
Creates a new process to execute an external command with given environment and current directory. |
Process |
exec(String[] cmdarray)
Creates a new process to execute an external command with given environment and current directory. |
Process |
exec(String[] cmdarray,
String[] envp)
Creates a new process to execute an external command with given environment and current directory. |
Process |
exec(String[] cmdarray,
String[] envp,
File dir)
|
Process |
exec(String command,
String[] envp)
Creates a new process to execute an external command with given environment and current directory. |
Process |
exec(String command,
String[] envp,
File dir)
Creates a new process to execute an external command with given environment and current directory. |
void |
exit(int status)
exit terminates the currently running Java application and the VM. |
long |
freeMemory()
freeMemory returns the amount of free memory in bytes. |
void |
gc()
gc indicates to the VM that it may be a good time to run the garbage collector. |
InputStream |
getLocalizedInputStream(InputStream in)
Deprecated. since 1.1, class InputStreamReader is available for converting the encoding. |
OutputStream |
getLocalizedOutputStream(OutputStream out)
Deprecated. since 1.1, class OutputStreamWriter is available for converting the encoding. |
static Runtime |
getRuntime()
getRuntime returns the singleton instance of this class. |
void |
halt(int status)
halt terminates the currently running Java application and the VM without running shutdown hooks. |
void |
load(String filename)
load a library or system file. |
void |
loadLibrary(String libname)
loadLibrary loads a system library with the given name into the system. |
long |
maxMemory()
maxMemory returns the maximum amount of memory that the virtual machine will attempt to use. |
boolean |
removeShutdownHook(Thread hook)
remove a shutdown hook that was registered using addShutdownHook. |
void |
runFinalization()
runFinalization runs the finalize() method of all objects that are pending for finalization. |
static void |
runFinalizersOnExit(boolean value)
Deprecated. running the finalizers is unsafe, therefore this method is deprecated. |
long |
totalMemory()
totalMemory returns the total size of the memory in bytes. |
void |
traceInstructions(boolean on)
Enable/disable tracing of executed instructions. |
void |
traceMethodCalls(boolean on)
Enable/disable tracing of method calls. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Runtime()
Method Detail |
---|
public long freeMemory()
Note that the free memory does not include memory that is no used by the application any longer, but that has not been reclaimed by the GC yet. For JamaicaVM, the GC usually reclaimes unused memory late as long as there is sufficient free memory available. For a more accurate result, on or several calls to gc() and runFinalization() may be used before the call to freeMemory().
For JamaicaVM, the returned value is the free memory that is directly usable for Java objects, i.e., memory needed by the memory management system for bookkeeping is not included.
public long totalMemory()
For JamaicaVM, the returned value is the memory that is directly usable for Java objects, i.e., memory needed by the memory management system for bookkeeping is not included.
public void gc()
For JamaicaVM, a call to Runtime.gc() will perform at least one full garbage collection cycle and at most two full garbage collection cycles. A call to gc() may therefore require a significant amount of time.
Note that running the garbage collector alone does not ensure that memory that is occupied by objects that have a finalize() method will be reclaimed: Their memory will be held until the finalize() method has been executed and the object is found to be unreachable after a second GC cycle. To ensure that all finalizers are executed and the memory of finalized objects is freed, three calls are required as follows:
Runtime.getRuntime().gc(); // find garbage Runtime.getRuntime().runFinalization(); // execute finalizers Runtime.getRuntime().gc(); // recycle finalized objects.
Any weak, soft or phantom references used in JamaicaVM will be updated also via the runFinalization(), i.e., an object to which a weak, soft or phantom reference exist can only be reclaimed when runFinalization() is called either by the finalizer thread or by an application thread,
To increase the overall system performance, it may be wise to run a low-priority thread that calls gc() and runFinalization() perpetually:
while (true) { Runtime.getRuntime().gc(); Runtime.getRuntime().runFinalization(); }
This will use all free CPU time to keep the amount of allocated memory as low as possible, such that memory allocation overhead for all higher-priority threads is minimized.
public long maxMemory()
For JamaicaVM, this is either the same as totalMemory if the heap size is not dynamic, else it is the maximum size to which the heap size may grow (the builder argument -maxHeapSize, -maxHeapSizeFromEnv or the VM environment variable JAMAICAVM_MAXHEAPSIZE).
public int availableProcessors()
For standard JamaicaVM, this method always returns 1. Even on a multi-processor system, JamaicaVM uses at most one processor to execute Java code. This might change in future releases.
For the parallel JamaicaVM (builder option -parallel, of command jamaicavmm), this gives the number of CPUs available to the VM for parallel execution of Java threads.
ensures
(result >= 1);
public static Runtime getRuntime()
InternalError
- for JamaicaVM, if this is called early
during system initialization before Runtime instance was created.public void exit(int status)
status
- exit status, 0 for ok.
SecurityException
- if a security manager is installed and
its checkExit(status) method throws this exception.public void halt(int status)
status
- exit status, 0 for ok.
SecurityException
- if a security manager is installed and
its checkExit(status) method throws this exception.public void addShutdownHook(Thread hook)
hook
- the shutdown hook, must not be null and must not be
or have been started.
SecurityManager
- if a security manager sm is installed and
a call to sm.checkPermission( new RuntimePermission(
"shutdownHooks" )) causes this exception.
IllegalArgumentException
- if hook has already been added
or if hook has already been started.
IllegalStateException
- if the VM is currently shutting
down.public boolean removeShutdownHook(Thread hook)
hook
- hook the shutdown hook, must not be null.
SecurityManager
- if a security manager sm is installed and
a call to sm.checkPermission( new RuntimePermission(
"shutdownHooks" )) causes this exception.
IllegalStateException
- if the VM is currently shutting
down.public static void runFinalizersOnExit(boolean value)
For JamaicaVM, the finalizer of boot classes are not run even if this routine was called with a true argument. The reason is that these finalizers would do things like closing System.out, which would make it very hard to do anything useful afterwards.
value
- flag
SecurityException
- if a security manager is installed and
its checkExit(0) method throws this exception.public Process exec(String command) throws IOException
If a security manager is installed, its checkExec() method will be called with the command array as an argument.
This is a short form for exec(command,null).
command
- the command and its arguments. This will be parsed
by a StringTokenizer and passed to exec(String[],String[],File).
IOException
- if an error during execution occurred. The
kinds of errors that may occur are system-dependent.
SecurityException
- if a security manager sm is installed
and sm.checkExec(cmdarray) throws this exception.
NullPointerException
- if cmdarray==null or any element of
cmdarray is null.
IllegalArgumentException
- if command contains no tokens.public Process exec(String command, String[] envp) throws IOException
If a security manager is installed, its checkExec() method will be called with the command array as an argument.
This is a short form for exec(command,envp,null).
command
- the command and its arguments. This will be parsed
by a StringTokenizer and passed to exec(String[],String[],File).envp
- environment variables, null to use the current
environment.
IOException
- if an error during execution occurred. The
kinds of errors that may occur are system-dependent.
SecurityException
- if a security manager sm is installed
and sm.checkExec(cmdarray) throws this exception.
NullPointerException
- if cmdarray==null or any element of
cmdarray is null or envp is non-null and any element of envp is
null.
IllegalArgumentException
- if command contains no tokens.public Process exec(String command, String[] envp, File dir) throws IOException
If a security manager is installed, its checkExec() method will be called with the command array as an argument.
command
- the command and its arguments. This will be parsed
by a StringTokenizer and passed to exec(String[],String[],File).envp
- environment variables, null to use the current
environment.dir
- the current directory to be used for the new process,
null to inherit the current directory from this process.
IOException
- if an error during execution occurred. The
kinds of errors that may occur are system-dependent.
SecurityException
- if a security manager sm is installed
and sm.checkExec(cmdarray) throws this exception.
NullPointerException
- if cmdarray==null or any element of
cmdarray is null or envp is non-null and any element of envp is
null.
IllegalArgumentException
- if command contains no tokens.public Process exec(String[] cmdarray) throws IOException
If a security manager is installed, its checkExec() method will be called with the command array as an argument.
This is a short form for exec(cmdarray,null).
cmdarray
- command array with the command and arguments of
the command line.
IOException
- if an error during execution occurred. The
kinds of errors that may occur are system-dependent.
SecurityException
- if a security manager sm is installed
and sm.checkExec(cmdarray) throws this exception.
NullPointerException
- if cmdarray==null or any element of
cmdarray is null.
IndexOutOfBoundsException
- if cmdarray.length==0.public Process exec(String[] cmdarray, String[] envp) throws IOException
If a security manager is installed, its checkExec() method will be called with the command array as an argument.
This is a short form for exec(cmdarray,envp,null).
cmdarray
- command array with the command and arguments of
the command line.envp
- environment variables, null to use the current
environment.
IOException
- if an error during execution occurred. The
kinds of errors that may occur are system-dependent.
SecurityException
- if a security manager sm is installed
and sm.checkExec(cmdarray) throws this exception.
NullPointerException
- if cmdarray==null or any element of
cmdarray is null or envp is non-null and any element of envp is
null.
IndexOutOfBoundsException
- if cmdarray.length==0.public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException
IOException
public void runFinalization()
Running the finalizers will permit the garbage collector to reclaim the memory occupied by these objects. However, the memory cannot be reclaimed immediately after execution of finalize(). Instead, since finalize() may have made the object reachable again, the garbage collector first has to show that no new references to the object have been generated. So in the worst case, the garbage collector needs to complete a full GC cycle before the memory can be reclaimed.
In JamaicaVM, runFinalization is wrapped into HeapMemory.executeInArea(), such that it can be called from scoped or immortal memory as well.
public void traceInstructions(boolean on)
The java specification does not specify what information should be collected or where it should be put. It actually allows Implementations to ignore this method completely.
NOTE: In JamaicaVM, this method does nothing.
on
- flag to indicate whether tracing should be enabled
(true) or disabled (false) trace instructions.public void traceMethodCalls(boolean on)
The java specification does not specify what information should be collected or where it should be put. It actually allows Implementations to ignore this method completely.
NOTE: In JamaicaVM, this method does nothing.
on
- flag to indicate whether tracing should be enabled
(true) or disabled (false) trace instructions.public void load(String filename)
If a security manager sm is installed, sm.checkLink(filename) is called to check the permission to load this library.
Repeated attempts to load the same library will have not effect.
filename
- absolute pathname of the library to loaded.
NullPointerException
- if filename is null
UnsatisfiedLinkError
- if the library could not be loaded
from the specified file.
SecurityException
- if a security manager sm is installed
and sm.checkLink(filename) throws this exception.public void loadLibrary(String libname)
If a security manager sm is installed, sm.checkLink(filename) is called to check the permission to load this library.
Repeated calls of this method have no effect, any library is loaded only once.
libname
- the library name, excluding path, suffix etc.
NullPointerException
- if libname is null
UnsatisfiedLinkError
- if the library with the specified
name could not be loaded.
SecurityException
- if a security manager sm is installed
and sm.checkLink(libname) throws this exception.public InputStream getLocalizedInputStream(InputStream in)
in
- the original input stream.
public OutputStream getLocalizedOutputStream(OutputStream out)
out
- the original output stream.
|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |