Thursday, August 19, 2010

Increasing Heap size of Java

Java heap is the heap size allocated to JVM applications which takes care of the new objects being created. If the objects being created exceed the heap size, it will throw an error saying memoryOutofBound

Java's default heap size limit is 128MB
. If you need more than this, you should use the -Xms and -Xmx command line arguments when launching your program:

java -Xms -Xmx


Java programs executes in JVM uses Heap of memory to manage the data. If your Java program requires a large amount of memory, it is possible that the virtual machine will begin to throw OutOfMemoryError instances when attempting to instantiate an object. The default heap size if 1 MB and can increase as much as 16 MB.

Setting/Increase JVM heap size

It is possible to increase heap size allocated by the Java Virtual Machine (JVM) by using command line options.

Following are few options available to change Heap Size.

1    -Xms        set initial Java heap size
2    -Xmx
       set maximum Java heap size
3    -Xss
        set java thread stack size

For example, you can set minimum heap to 64 MB and maximum heap 256 MB for a Java program HelloWorld.

java -Xms64m -Xmx256m HelloWorld

Getting / Reading default heap size

It is possible to read the default JVM heap size programmatically by using totalMemory() method of Runtime class. Use following code to read JVM heap size.
    public class GetHeapSize {
        public static void main(String[]args){
    
            //Get the jvm heap size.
            long heapSize = Runtime.getRuntime().totalMemory();
    
            //Print the jvm heap size.
            System.out.println("Heap Size = " + heapSize);
        }
    }

No comments:

Post a Comment