Runtime
The Runtime class encapsulates the run-time environment. You cannot
instantiate a Runtime object.
However, you can get a reference to the current Runtime object by calling the static method Runtime.getRuntime( ). Once you obtain a reference to the current Runtime object, you can call several
methods that control the state and behavior of the Java Virtual Machine.
Applets and other untrusted code typically cannot call any of the Runtime methods without raising a SecurityException. Several commonly
used methods defined by Runtime are
shown in Table 17-11.
Let’s look at two of the most
common uses of the Runtime class:
memory management and executing additional processes.
Memory
Management
Although Java provides
automatic garbage collection, sometimes you will want to know how large the
object heap is and how much of it is left. You can use this information, for
example, to check your code for efficiency or to approximate how many more
objects of a certain type can be instantiated. To obtain these values, use the totalMemory( ) and freeMemory( ) methods.
As mentioned in Part I,
Java’s garbage collector runs periodically to recycle unused objects. However,
sometimes you will want to collect discarded objects prior to the collector’s
next appointed rounds. You can run the garbage collector on demand by calling
the gc( ) method. A good thing to
try is to call gc( ) and then call freeMemory( ) to get a baseline memory
usage. Next, execute your code and call freeMemory(
) again to see how much memory it is allocating. The following program
illustrates this idea:
// Demonstrate totalMemory(), freeMemory() and
gc().
class MemoryDemo {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime(); long mem1, mem2;
Integer someints[] = new Integer[1000];
System.out.println("Total memory is:
" + r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free memory:
" + mem1); r.gc();
mem1 = r.freeMemory();
System.out.println("Free memory after
garbage collection: " + mem1);
for(int i=0; i<1000; i++)
someints[i] = new Integer(i); // allocate
integers
mem2 = r.freeMemory();
System.out.println("Free memory after
allocation: " + mem2);
System.out.println("Memory used by
allocation: " + (mem1-mem2));
// discard Integers
for(int i=0; i<1000; i++) someints[i] = null;
r.gc(); // request garbage collection
mem2 = r.freeMemory();
System.out.println("Free memory after
collecting" + " discarded Integers: " + mem2);
}
}
Sample output from this
program is shown here (of course, your actual results may vary):
Total memory is: 1048568
Initial free memory: 751392
Free memory after garbage collection: 841424
Free memory after allocation: 824000
Memory used by allocation: 17424
Free memory after collecting discarded
Integers: 842640
Executing
Other Programs
In safe environments, you can
use Java to execute other heavyweight processes (that is, programs) on your
multitasking operating system. Several forms of the exec( ) method allow you to name the program you want to run as
well as its input parameters. The exec(
) method returns a Process
object, which can then be used to control how your Java program interacts with
this new running process. Because Java can run on a variety of platforms and
under a variety of operating systems, exec(
) is inherently environment-dependent.
The following example uses exec( ) to launch notepad, Windows’ simple text editor. Obviously, this example must
be run under the Windows operating system.
// Demonstrate exec().
class ExecDemo {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime(); Process p = null;
try {
p = r.exec("notepad");
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
}
}
There are several alternative
forms of exec( ), but the one shown
in the example is the most common. The Process
object returned by exec( ) can be
manipulated by Process’ methods
after the new program starts running. You can kill the subprocess with the destroy( ) method. The waitFor( ) method causes your program
to wait until the subprocess finishes.
The exitValue( ) method returns the
value returned by the subprocess when it is finished. This is typically 0 if no
problems occur. Here is the preceding exec(
) example modified to wait for the running process to exit:
// Wait until notepad is terminated.
class ExecDemoFini {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime(); Process p = null;
try {
p = r.exec("notepad"); p.waitFor();
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
System.out.println("Notepad returned
" + p.exitValue());
}
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.