Java Runtime class details _ Power node Java College collation

  • 2020-07-21 07:37:03
  • OfStack

1. An overview of the

The Runtime class encapsulates the runtime environment. Each Java application has an instance of the Runtime class that enables the application to connect to the environment in which it is running. You generally cannot instantiate an Runtime object, nor can an application create its own instance of the Runtime class, but you can get a reference to the current Runtime runtime object through the getRuntime method. Once you have a reference to the current Runtime object, you can call the methods of the Runtime object to control the state and behavior of the Java virtual machine. An SecurityException exception is often raised when untrusted code calls any Runtime method.

2. Common applications

1. Memory management:

Java provides automatic collection of unwanted units. The totalMemory() and freeMemory() methods tell you how much heap memory an object has and how much is left. Java periodically collects garbage objects (unused objects) to free up memory space. But if you want to collect the discarded objects before the next specified period of the collector, you can run the garbage collector as needed by calling the gc() method. A good experiment is to call the gc() method first, then the freeMemory() method to see the basic memory usage, then execute the code, then call the freeMemory() method again to see how much memory is allocated. The following program demonstrates this idea.


// This example is from the java Core Technologies volume 1
 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 is : " + mem1); 
        r.gc(); 
        mem1 = r.freeMemory(); 
        System.out.println("Free memory after garbage collection : " + mem1); 
        //allocate integers 
        for(int i=0; i<1000; i++) someints[i] = new Integer(i);  
        mem2 = r.freeMemory(); 
        System.out.println("Free memory after allocation : " + mem2); 
        System.out.println("Memory used by allocation : " +(mem1-mem2));  
        //discard Intergers 
        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); 
    } 
}

The results after compilation are as follows (the results run by different machines at different times are not identical) :


Total memory is  : 2031616
Initial free is : 1818488
Free memory after garbage collection : 1888808
Free memory after allocation : 1872224
Memory used by allocation : 16584
Free memory after collecting discarded integers : 1888808

2. Execute other procedures

In a secure environment, Java can be used in a multitasking operating system to execute other particularly large processes (that is, programs). The ecec() method has several forms for naming the program you want to run and its input parameters. The ecec() method returns an Process object that you can use to control how the Java program interacts with the newly running process. The ecec() method is essentially context-dependent.

The following example is to start windows's Notepad notepad using the ecec() method. This example must run on the Windows operating system.


// This example is from the Java Core Technologies volume 1
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 other forms of ecec(), one of which is shown in this example. After the ecec() method returns the Process object, it is ready to use the Process method once the new program has started running. You can kill the child process using the destory() method, or you can wait until the subroutine ends using the waitFor() method, which returns the value returned when the child process ends. If there are no errors, 0 is returned, otherwise non-0 is returned. The following is an improved version of an example of the ecec() method. The example is modified to wait until the running process exits:


// This example is from the Java Core Technologies volume 1
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());
  }
}

Here is the result of the run (when Notepad is closed, the program is then run to print the information) :

Notepad returned 0

Please press any key to continue..

While the child process is running, standard I/O can be read and written. The getOutputStream() and getInPutStream() methods return the standard input and output for the child process.


Related articles: