Monday 1 February 2016

How to calculate the heap size using Java program?

Calculate the heap size using Java program

To calculate the default JVM heap size programmatically, we can use totalMemory() method of Runtime class.

package com.java.tricks;
public class JavaHeapSize {
     public static void main(String[] args) {

           // JVM heap size.
           long heapSize = Runtime.getRuntime().totalMemory();

           System.out.println("Heap Size in bytes = " + heapSize);
     }
}
Output: Heap Size in bytes = 16252928


long java.lang.Runtime.totalMemory():

Returns total amount of memory currently available in bytes.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...