Java Common Class Library--runtime

Source: Internet
Author: User

Runtime is a class that encapsulates a JVM process, and each Java program actually initiates a JVM process, and each JVM corresponds to a runtime instance. This instance is instantiated by the JVM.

  There is no construction method in the definition of this class, because the methods of this class are privatized. Then there must be a method in this class that returns the instantiated object (similar to the Singleton designer pattern).

Instantiation method:

Runtime run = Runtime.getruntime ();    Instantiate operations by using the static method of the runtime class

Once an instance is obtained, the above method can be manipulated.

One, get JVM information

Each runtime () is instantiated by the JVM, so you can get some information directly from this class.

 Public classruntimedemo01{ Public Static voidMain (String args[]) { Runtime run = Runtime.getruntime (); Instantiate operations by using the static method of the runtime classSystem.out.println ("JVM Maximum amount of memory:" +run.maxmemory ()) ;// Observe the maximum memory , depending on the machine, the environment will be differentSYSTEM.OUT.PRINTLN ("JVM Idle Memory:" +run.freememory ()) ;//get free memory run by programString str = "Hello" + "world" + "!!!" + "\ T" + "Welcome" + "to" + "MLDN" + "~" ;        System.out.println (str);  for(intx=0;x<1000;x++) { str + = x; Cyclic modification of content will result in multiple garbage} System.out.println ("The amount of JVM idle memory after manipulating a string:" + run.freememory ());        Run.gc (); //garbage collection, free spaceSystem.out.println ("After garbage collection, JVM idle memory:" +run.freememory ()); }};

Operation Result:

JVM Maximum amount of memory: 1394606080JVM Idle memory:93386136HelloWorld!!!    Welcome to mldn~ after manipulating a string, the amount of idlememory in the JVM: 87346392 after garbage collection, the amount of idle memory in the JVM:93846784
Runtime is used in conjunction with the Process class

In addition to observing memory usage, you can run native executable programs directly using the runtime class, for example, Notepad .

。 Public Process exec (String Command)throws IOException;

It can be seen that this method requires exception handling .

 PackageThread1; Public classdemo1{ Public Static voidMain (String args[]) {Runtime run= Runtime.getruntime ();//gets the instantiated object of the runtime class        Try{run.exec ("Eclipse.exe");//Call Native program, this method requires exception handling}Catch(Exception e) {e.printstacktrace (); //Print exception Information//System.out.println (e);        }    }};

Operation Result:

The eclipse program is called.

The above program just opened Notepad, now requires Notepad to open 5 seconds after the automatic shutdown, if you want to operate to achieve the above functions, you need to operate the process. Because each Notepad opens, there is a process.

The return value of the Exec () method is process, which represents the action class for a process. A process can be destroyed by the destroy () method. And let the thread pause for 5 seconds through the sleep () method.

 PackageThread1;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream; Public classdemo1{ Public Static voidMain (String args[]) {Runtime T=Runtime.getruntime (); Process P  =null;        //define process variables, Note that here is a declaration of an object that is not instantiated and is instantiated in the following assignment.         Try{ P =t.exec ("Eclipse.exe"); The Exec () method return value is the process object. Call Native program, this method requires exception handling}Catch(IOException e) {e.printstacktrace (); //Print exception Information        }        Try{ Thread.Sleep ( 5000); Keep this thread alive for 5 seconds}Catch(Exception e) {} P.destroy (); //End this process    }};

Operation Result:

You will notice that the Eclipse program automatically shuts down after 5 seconds of opening.

Summarize

The runtime class itself is an application of a single case design. Because there is only one runtime () object for the entire JVM, you can use the runtime () class to obtain system information for the JVM. or use GC () to release the system's garbage memory.

You can also use this class to run native programs.

  

Java Common Class Library--runtime

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.