Saturday, September 27, 2014

Simple Example1


public class HeapSize {
   
   
 // Get current size of heap in bytes
    long heapSize = Runtime.getRuntime().totalMemory();

    // Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
    long heapMaxSize = Runtime.getRuntime().maxMemory();

     // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
    long heapFreeSize = Runtime.getRuntime().freeMemory();
   
   


public static void main(String [] args) {

 

    int processors = Runtime.getRuntime().availableProcessors();
   
    int mb = 1024*1024;
   
    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();
   
    System.out.println("##### Heap utilization statistics [MB] #####");
   
    //Print used memory
    System.out.println("Used Memory:"
        + (runtime.totalMemory() - runtime.freeMemory()) / mb);

    //Print free memory
    System.out.println("Free Memory:"
        + runtime.freeMemory() / mb);
   
    //Print total available memory
    System.out.println("Total Memory:" + runtime.totalMemory() / mb);

    //Print Maximum available memory
    System.out.println("Max Memory:" + runtime.maxMemory() / mb);
}
}

No comments:

Post a Comment