How to change/increase the heap size of the Java Virtual Machine in Java?

JavaObject Oriented ProgrammingProgramming

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


Live Demo 

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);


Heap Size: 16252928
Updated on 11-Feb-2020 10:21:05