Java by API/java.lang/Runtime

Материал из Java эксперт
Перейти к: навигация, поиск

Runtime: addShutdownHook(Thread hook)

   <source lang="java">

public class MainClass {

 public static void main(String[] args) throws Exception {
   Object f = new Object() {
     public void finalize() {
       System.out.println("Running finalize()");
     }
   };
   Runtime.getRuntime().addShutdownHook(new Thread() {
     public void run() {
       System.out.println("Running Shutdown Hook");
     }
   });
   f = null;
   System.gc();
   System.out.println("Calling System.exit()");
   System.exit(0);
 }

}


 </source>
   
  
 
  



Runtime: availableProcessors()

   <source lang="java">
 

public class Main {

 public static void main(String[] args) {
   Runtime runtime = Runtime.getRuntime();
   int nrOfProcessors = runtime.availableProcessors();
   System.out.println("Number of processors available to the Java Virtual Machine: "
       + nrOfProcessors);
 }

} // Number of processors available to the Java Virtual Machine: 2


 </source>
   
  
 
  



Runtime: exec(String[] command)

   <source lang="java">

/*

* Output:
* 
*   
* 
*  
*/

public class MainClass {

 public static void main(String args[]) {
 Runtime r = Runtime.getRuntime();
 Process p = null;
 String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
 try {
     p = r.exec(cmd);
 } catch (Exception e) {
     System.out.println("error executing " + cmd[0]);
 }
 }

}


 </source>
   
  
 
  



Runtime: exec(String command) (2)

   <source lang="java">

/*

* Output: 
*/

public class MainClass {

 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.");
   }
 }

}


 </source>
   
  
 
  



Runtime: freeMemory()

   <source lang="java">

/*

* Output: 

Total memory is: 2031616 Initial free memory: 1774312 Free memory after garbage collection: 1875680 Free memory after allocation: 1715296 Memory used by allocation: 160384 Free memory after collecting discarded Integers: 1875680

*/

public class MainClass {

 public static void main(String args[]) {
   Runtime r = Runtime.getRuntime();
   long mem1, mem2;
   Integer someints[] = new Integer[10000];
   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 < someints.length; 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));
   for (int i = 0; i < someints.length; i++)
     someints[i] = null;
   r.gc(); // request garbage collection
   mem2 = r.freeMemory();
   System.out.println("Free memory after collecting" + " discarded Integers: "
       + mem2);
 }

}


 </source>
   
  
 
  



Runtime: gc()

   <source lang="java">

/*

* Output: 

Total memory is: 2031616 Initial free memory: 1774312 Free memory after garbage collection: 1875680 Free memory after allocation: 1715296 Memory used by allocation: 160384 Free memory after collecting discarded Integers: 1875680

*/

public class MainClass {

 public static void main(String args[]) {
   Runtime r = Runtime.getRuntime();
   long mem1, mem2;
   Integer someints[] = new Integer[10000];
   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 < someints.length; 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));
   for (int i = 0; i < someints.length; i++)
     someints[i] = null;
   r.gc(); // request garbage collection
   mem2 = r.freeMemory();
   System.out.println("Free memory after collecting" + " discarded Integers: "
       + mem2);
 }

}


 </source>
   
  
 
  



Runtime.getRuntime()

   <source lang="java">

/*

* Output:
* 
*   
* 
*  
*/

public class MainClass {

 public static void main(String args[]) {
 Runtime r = Runtime.getRuntime();
 Process p = null;
 String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
 try {
     p = r.exec(cmd);
 } catch (Exception e) {
     System.out.println("error executing " + cmd[0]);
 }
 }

}


 </source>
   
  
 
  



Runtime: maxMemory()

   <source lang="java">
 

public class Main {

 public static void main(String[] argv) throws Exception {
   long heapSize = Runtime.getRuntime().totalMemory();
   long heapMaxSize = Runtime.getRuntime().maxMemory();
   long heapFreeSize = Runtime.getRuntime().freeMemory();
 }

}


 </source>
   
  
 
  



Runtime: totalMemory()

   <source lang="java">

/*

* Output: 

Total memory is: 2031616 Initial free memory: 1774312 Free memory after garbage collection: 1875680 Free memory after allocation: 1715296 Memory used by allocation: 160384 Free memory after collecting discarded Integers: 1875680

*/

public class MainClass {

 public static void main(String args[]) {
   Runtime r = Runtime.getRuntime();
   long mem1, mem2;
   Integer someints[] = new Integer[10000];
   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 < someints.length; 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));
   for (int i = 0; i < someints.length; i++)
     someints[i] = null;
   r.gc(); // request garbage collection
   mem2 = r.freeMemory();
   System.out.println("Free memory after collecting" + " discarded Integers: "
       + mem2);
 }

}


 </source>