Class Runtime in Java

digitization, transformation, binary

Class Runtime in Java

Every Java application has a single instance of class Runtime. Runtime class allows the application to interact with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method as an application can not create its own instance of this class.

Runtime class encapsulates a number of platform-dependent system functions. The static method getRuntime() returns the Runtime object for the current platform, and this object can be used to perform system functions in a platform-independent way.

Ads code goes here

The Runtime class provides access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program. The Runtime class does not have any public constructors, so a program cannot create its own instances of the class.

A program must call the getRuntime() method to get a reference to the current Runtime object. Information about operating system features is accessible through the System class.

exec() starts a new process running externally to the interpreter.

exit() causes the Java interpreter to exit and return a specified return code.

freeMemory() returns the approximate amount of free memory.

totalMemory() returns the total amount of memory available to the Java interpreter.

gc() forces the garbage collector to run synchronously, which may free up more memory.

runFinalization() forces the finalize() methods of unreferenced objects to be run immediately. This may free up system resources that those objects were holding.

load() loads a dynamic library with a fully-specified pathname.

loadLibrary() loads a dynamic library with only the library name specified; it looks in platform-dependent locations for the specified library. These libraries generally contain native code definitions for native methods.

traceInstructions() and traceMethodCalls() enable and disable tracing by the interpreter.

Note

  • some of the Runtime methods are more commonly called via the static methods of the System class.
  • Any processes run outside of Java may be system-dependent.

The structure of the class-Runtime is as follows :

public class java.lang.Runtime extends java.lang.Object{
//methods:
public Process exec(String command) throws IOException;;
public Process exec(String command,String exp[]) throws IOException;;
public Process exec(String commandArray[])throws IOException;;
public Process exec(String commandArray[],String exp[])throws IOException;;
public void exit(int status);
public native long freeMemory();
public native void gc();
public InputStream getLocalizedInputStream(InputStream in);
public OutputStream getLocalizedOutputStream(OutputStream out);
public static Runtime getRuntime();
public synchronized void load(String fileName);
public synchronized void loadLibrary(String libName);
public native void runFinalization();
public native long totalMemory();
public native void traceInstruction(boolean on);
public native void traceMethodCalls(boolean on);
public static void runFinalizersOnExit(boolean value);
}
 

The details of the class structure are given as follows:

public static Runtime getRuntime();

public static Runtime getRuntime() method returns a reference to the current Runtime object. Because the other methods of the Runtime class are not static, a program must call this method first in order to get a reference to a Runtime object that can be used in calling the other methods.

This method returns a reference to the current Runtime object.

public static void runFinalizersOnExit(boolean value); 

public static void runFinalizersOnExit(boolean value); method specifies whether or not the finalize() methods of all objects that have to finalize() methods are run before the Java virtual machine exits. By default, the finalizers are not run on exit.

READ  Class Date in Java

Parameter
value – A boolean value that specifies whether or not finalization occurs on exit.

public Process exec(String command);

public Process exec(String command) method starts a new process to execute the given external command. The standard input, standard output, and standard error streams from the process are redirected to OutputStream and InputStream objects that are accessible through the Process object returned by this method. Calling this method is equivalent to:
exec(command, null)

This method returns a Process object that controls the process started by this method.

Parameter
command – A string that contains the name of an external command and any arguments to be passed to it.

public Process exec(String command, String envp[]);

public Process exec(String command, String envp[]) method starts a new process to execute the given external command. The standard input, standard output, and standard error streams from the process are redirected to OutputStream and InputStream objects that are accessible through the Process object returned by this method.

The method parses the command string into words that are separated by whitespace. It creates a String object for each word and places word String objects into an array. If that array is called commandArray, calling this method is equivalent to:
exec(commmandArray, envp)

This method returns a Process object that controls the process started by this method.

Parameter
command – A string that contains the name of an external command and any arguments to be passed to it.
envp – An array of strings that specifies the values for the environment variables of the new process. Each String in the array should be of the form variableName =value. If envp is null, the values of the environment variables in the current process are copied to the new process.

public Process exec(String cmdarray[]);

public Process exec(String cmdarray[]) method starts a new process to execute the given external command. The standard input, standard output, and standard error streams from the process are redirected to OutputStream and InputStream objects that are accessible through the Process object returned by this method. Calling this method is equivalent to:
exec(commandArray, null)

This method returns a Process object that controls the process started by this method.

Parameter
commandArray – An array of strings that contains separate strings for the name of an external command and any arguments to be passed to it. The first string in the array must be the command name.

public Process exec(String cmdarray[], String envp[]);

public Process exec(String cmdarray[], String envp[]) method starts a new process to execute the given external command. The standard input, standard output, and standard error streams from the process are redirected to OutputStream and InputStream objects that are accessible through the Process object returned by this method.

This method returns a Process object that controls the process started by this method.

Parameter
commandArray – An array of strings that contains separate strings for the name of an external command and any arguments to be passed to it. The first string in the array must be the command name.
envp – An array of strings that specifies the values for the environment variables of the new process. Each String in the array should be of the form variableName =value. If envp is null, the values of the environment variables in the current process are copied to the new process.

public void exit(int status);

public void exit(int status) method causes the Java virtual machine to exit with the given status code. By convention, a nonzero status code indicates abnormal termination. This method never returns.

READ  Old Fashioned Coding Style May Help Garbage Collector to Reclaim Memory Quickly

Parameter
status – The exit status code to use.

public native long freeMemory();

public native long freeMemory() method returns an estimate of the number of free bytes in system memory. The value returned by this method is always less than the value returned by totalMemory(). Additional memory may be freed by calling the gc() method.

This method returns an estimate of the number of free bytes in system memory.

public native void gc();

public native void gc() method causes the Java virtual machine to run the garbage collector in the current thread.

The garbage collector finds objects that will never be used again because there are no live references to them. After it finds these objects, the garbage collector frees the storage occupied by these objects.

The garbage collector is normally run continuously in a thread with the lowest possible priority so that it works intermittently to reclaim storage. The gc() method allows a program to invoke the garbage collector explicitly when necessary.

public InputStream getLocalizedInputStream(InputStream in);

public InputStream getLocalizedInputStream(InputStream in) method returns an InputStream object that converts characters from the local character set to Unicode. For example, if the InputStream uses an 8-bit character set with values less than 128 representing Cyrillic letters, this method maps those characters to the corresponding Unicode characters in the range ‘\u0400’ to ‘\u04FF’.

This method is deprecated as of JDK 1.1. You should instead use the new InputStreamReader and BufferedReader classes to convert characters from the local character set to Unicode.

This method returns the localized InputStream.

Parameter
in – An InputStream object that is to be localized.

public OutputStream getLocalizedOutputStream(OutputStream out); 

public OutputStream getLocalizedOutputStream(OutputStream out); method returns an OutputStream object that converts characters from Unicode to the local character set. For example, if the local character set is an 8-bit character set with values less than 128 representing Cyrillic letters, this method maps Unicode characters in the range ‘\u0400’ to ‘\u04FF’ to the appropriate characters in the local character set.

This method is deprecated now. We should instead use the new OutputStreamWriter and BufferedWriter classes to convert characters from Unicode to the local character set.

This method returns the localized OutputStream.

Parameter
out – An OutputStream object that is to be localized.

public synchronized void load(String filename);

public synchronized void load(String filename) method loads the specified dynamically linked library. It is often more convenient to call the load() method of the System class because it does not require getting a Runtime object.

Parameter
filename – A string that specifies the complete path of the file to be loaded.

public synchronized void loadLibrary(String libname);

public synchronized void loadLibrary(String libname) method loads the specified dynamically linked library. It looks for the specified library in a platform-specific way.

It is often more convenient to call the loadLibrary() method of the System class because it does not require getting a Runtime object.

READ  What is a Compilation Unit in Java?

Parameter
libname – A string that specifies the name of a dynamically linked library.

public native void runFinalization();

public native void runFinalization() method causes the Java virtual machine to run the finalize() methods of any objects in the finalization queue in the current thread.

When the garbage collector discovers that there are no references to an object, it checks to see if the object has a finalize() method that has never been called. If the object has such a finalize() method, the object is placed in the finalization queue. While there is a reference to the object in the finalization queue, the object is no longer considered garbage-collectible.

Normally, the objects in the finalization queue are handled by a separate finalization thread that runs continuously at a very low priority. The finalization thread removes an object from the queue and calls its finalize() method. As long as the finalize() method does not generate a reference to the object, the object again becomes available for garbage collection.

Because the finalization thread runs at a very low priority, there may be a long delay from the time that an object is put on the finalization queue until the time that its finalize() method is called. The runFinalization() method allows a program to run the finalize() methods explicitly. This can be useful when there is a shortage of some resource that is released by a finalize() method.

public native long totalMemory();

public native long totalMemory() method returns the total number of bytes in system memory in the Java virtual machine. The total includes the number of bytes of memory being used by allocated objects, as well as the number of free bytes available for allocating additional objects. An estimate of the number of free bytes in system memory is available through the freeMemory() method.

This method returns the total number of bytes in system memory.

public native void traceInstructions(boolean on);

public native void traceInstructions(boolean on) method controls whether or not the Java virtual machine outputs a detailed trace of each instruction that is executed. The boolean parameter causes tracing to be turned on or off. The tracing of instructions is only possible in a Java virtual machine that was compiled with the tracing option enabled. Production releases of the Java virtual machine are generally not compiled with tracing enabled.

Parameter
on – A boolean value that specifies if instructions are to be traced. true if instructions are to be traced; otherwise false.

public native void traceMethodCalls(boolean on);

public native void traceMethodCalls(boolean on) method controls whether or not the Java virtual machine outputs a detailed trace of each method that is invoked. The boolean parameter causes tracing to be turned on or off. The tracing of instructions is only possible in a Java virtual machine that was compiled with the tracing option enabled. Production releases of the Java virtual machine are generally not compiled with tracing enabled.

Parameter
on – A boolean value that specifies if method calls are to be traced. true if instructions are to be traced; otherwise false.

Apart from these Runtime class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *