A Java program can execute in the Java Virtual Machine (JVM) uses the heap memory to manage the data. If our Java program requires more memory, there is a possibility that the Java Virtual Machine(JVM) will begin to throw OutOfMemoryError instances when attempting to instantiate an object in Java.
To Change/increase the JVM Heap Size
In Java, It is possible to increase the heap size allocated by the JVM using command line options
- -Xms - Set an initial Java heap size
- -Xmx - Set the maximum Java heap size
- -Xss - Set the Java thread stack size
Example
public class HeapSizeTest {
public static void main(String[]args){
// To get the JVM Heap Size
long heapSize = Runtime.getRuntime().totalMemory();
// To print the JVM Heap Size
System.out.println("Heap Size: " + heapSize);
}
}Output
Heap Size: 16252928