Study on Java--runtime class

Source: Internet
Author: User

The runtime class represents the runtime environment of a Java program, each Java program has a corresponding runtime instance, which is connected to the runtime environment by the application, and the application cannot create its own runtime instance, but can pass the GetRuntime () method to get the runtime object associated with it.

1.Runtime represents the runtime environment of a Java program and can access information about the JVM, such as the number of processors and memory information.

For example, the following code:

public class Runtimedemo01{public static void Main (String args[]) {Runtime run = runtime.getruntime ();// Instantiate operations System.out.println ("JVM Maximum amount of memory:" + run.maxmemory ()) through the static method of the runtime class;//Observe the maximum RAM, depending on the machine, The environment will also be different System.out.println ("JVM Idle Memory:" + run.freememory ());//Get the program to run free RAM string str = "Hello" + "world" + "!!!" + "\ T" + "Welcome" + "to" + "JAVA" + "~"; System.out.println (str); for (int x=0;x<1000;x++) {str + = x;//loop modify content, resulting in multiple garbage}system.out.println ("after the action string, JVM Idle Memory: "+ run.freememory ()); Run.gc ();//garbage collection, free space System.out.println (" After garbage collection, JVM idle memory: "+ run.freememory ());}};
Run as follows:

JVM Maximum amount of memory: 1895825408
JVM Idle Memory: 126929912
Hello World!!! Welcome to java~
JVM Idle memory After manipulating string: 120791056
After garbage collection, the amount of idle memory in the JVM: 127760264


The. Exec () method in the 2.Runtime class, you can call the native program, take the Notepad program as an example:

Import Java.io.ioexception;public class Runtimedemo {public static void main (string[] args) {Runtime run= Runtime.getruntime (); Process Pro=null;try {pro=run.exec ("notepad.exe");//Call Notepad program} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} try {thread.sleep (3000);//Three Seconds timed} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Pro.destroy ();//Process End}}

After running, the Notepad will pop up and automatically close after three seconds.

Study on Java--runtime class

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.