img
Method
Description
Removes thrd from the list of threads to run when
boolean
removeShutdownHook(Thread thrd)
the Java Vir tual Machine terminates. It returns true if
successful--that is, if the thread was removed.
void runFinalization( )
Initiates calls to the finalize( ) methods of unused but
not yet recycled objects.
long totalMemor y( )
Returns the total number of bytes of memor y available
to the program.
void traceInstructions(boolean traceOn)
Turns on or of f instruction tracing, depending upon
the value of traceOn. If traceOn is true, the trace is
displayed. If it is false, tracing is turned of f.
void traceMethodCalls(boolean traceOn) Turns on or of f method call tracing, depending upon
the value of traceOn. If traceOn is true, the trace is
displayed. If it is false, tracing is turned of f.
TABLE 16-11
A Sampling of Methods Defined by Runtime (continued)
Let's look at two of the most common uses of the Runtime class: memory management
and executing additional processes.
Memor y 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());
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home