Getting the Size of the Java Memory Heap - Java Java Virtual Machine

Java examples for Java Virtual Machine:Utility

Description

Getting the Size of the Java Memory Heap

Demo Code


public class Main {

  public void main(String[] argv) {
    // 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.
    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();
  }//from   w  ww.  j  a  v  a2 s .c om
}

Related Tutorials