Showing posts with label Runtime JVM. Show all posts
Showing posts with label Runtime JVM. Show all posts

Tuesday, September 6, 2011

Get Total Memory of Java Virtual Machine(JVM)

    /*
    Get Total Memory of Java Virtual Machine(JVM) Example
    This Java example shows how to get amount of total memory
    in the Java Virtual Machine(JVM) using totalMemory method
    of Java Runtime class.
    */

    package org.best.example;

    public class GetTotalMemory {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * To determine amount of total memory available to current
    * Java Virtual Machine(JVM), use
    *
    * long totalMemory()
    * method of Runtime class.
    */
    
    long totalMemory = runtime.totalMemory();
    
    System.out.println(totalMemory + " bytes total in JVM");
    }
    
    
    }
    
    /*
    Typical output would be
    5177344 bytes total in JVM
    */

Monday, September 5, 2011

Terminate Java Virtual Machine (JVM)

    /*
    Terminate Java Virtual Machine (JVM) Example
    This Java example shows how to terminate currently running
    Java Virtual Machine(JVM) using halt method of Runtime class.
    
    */

    package org.best.example;
    
    public class TerminateVirtualMachine {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * To forcibly terminate the currently running virtual machine, use
    *
    * void halt(int status)
    * method of Runtime class.
    *
    * Usually, non-zero status is passed as status for abnormal exit.
    *
    * Please note that, unlike exit method, this method DOES NOT invoke
    * shutdown hooks or run object finalizers.
    *
    *
    */
    
    System.out.println("About to halt the current jvm");
    runtime.halt(1);
    
    /*
    * THIS METHOD NEVER RETURNS NORMALLY.
    * This statement will never executed, as the JVM is
    * terminated!
    */
    System.out.println("JVM Terminated");
    }
    
    }
    
    /*
    Typical output would be
    About to halt the current jvm
    */

Sunday, September 4, 2011

Suggest JVM to Run Object Finalization

    /*
    Suggest JVM to Run Object Finalization Example
    This Java example shows how to suggest the JVM to run discarded
    object's finalization methods.
    
    */
    
    package org.best.example;

    public class RunFinalization {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * To suggest the JVM to run discarded object's finalization
    * methods, use
    *
    * void runFinalization()
    * method of Runtime class.
    *
    */
    
    runtime.runFinalization();
    }
    
    }

Saturday, September 3, 2011

Suggest JVM to Run Garbage Collector

     /*
    Suggest JVM to Run Garbage Collector Example
    This Java example shows how to suggest the JVM to run garbage
    collection using gc method of Runtime class.
   
    */

    package org.best.example;

    public class RunGarbageCollector {
   
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
   
    /*
    * To suggest the JVM to run garbage collector, use
    *
    * void gc()
    * method of Runtime class.
    *
    */
   
    runtime.gc();
    System.out.println("JVM has made best effort to garbage collect!");
    }
   
    }
   
    /*
    Output of this program would be
    JVM has made best effort to garbage collect!
    */

Friday, September 2, 2011

Get Maximum Memory Available to Java Virtual Machine(JVM)

    /*
    Get Maximum Memory Available to Java Virtual Machine(JVM) Example
    This Java example shows how to get maximum amount memory
    Java Virtual Machine will attempt to use using maxMemory()
    method of Runtime class.
    */

    package org.best.example;
    
    public class GetMaxMemory {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * To determine amount of maximum memory Java Virtual
    * Machine (JVM) will attempt to use, use
    *
    * long maxMemory()
    * method of Runtime class.
    *
    * If there is no limit inherited, value of Long.MAX_VALUE
    * will be returned.
    */
    
    long maxMemory = runtime.maxMemory();
    
    System.out.println(maxMemory + " bytes max");
    }
    
    }
    
    /*
    Typical output would be
    66650112 bytes max
    */

Thursday, September 1, 2011

Get Free Memory of Java Virtual Machine(JVM)

    /*
    Get Free Memory of Java Virtual Machine(JVM) Example
    This Java example shows how to get amount of free memory
    in the Java Virtual Machine(JVM) using freeMemory method
    of Java Runtime class.
    */

    package org.best.example;

    public class GetFreeMemory {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * To determine amount of free memory available to current
    * Java Virtual Machine(JVM), use
    *
    * long freeMemory()
    * method of Runtime class.
    */
    
    long freeMemory = runtime.freeMemory();
    
    System.out.println(freeMemory + " bytes free in JVM");
    }
    
    
    }
    
    /*
    Typical output would be
    4993048 bytes free in JVM
    */

Wednesday, August 31, 2011

Get Java Runtime

    /*
    Get Java Runtime Example
    This get java runtime examples shows how to get the runtime
    in which the java application is running. Runtime allows us
    to interface with the environment in which the current
    application is running.
    */
    
    package org.best.example;
    public class GetJavaRuntime {
    
    public static void main(String args[])
    {
    /*
    * Current runtime can be obtained using getRuntime method
    * of Runtime class.
    *
    * getRuntime method is a static method. Application can not
    * create instance of this class.
    */
    
    Runtime runtime = Runtime.getRuntime();
    
    }
    }
    
    /*
    Output of this program would be,
    Current java runtime obtained!
    */

Tuesday, August 30, 2011

Get Available Processors using Java Runtime

    /*
    Get Available Processors using Java Runtime Example
    This Java example shows how to get number of processors
    available to current Java Virtual Machine (JVM).
    */
   
    package org.best.example;

    public class GetAvailableProcessors {
    
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
    
    /*
    * use availableProcessors method to determine
    * how many processors are available to the Java Virtual
    * Machine (JVM).
    */
    
    int numberOfProcessors = runtime.availableProcessors();
    
    System.out.println(numberOfProcessors + " processor available to JVM");
    }
    }
    
    /*
    Typical output of this program would be,
    1 processor available to JVM
    */