Translate

Monday, October 29, 2012

How to increase JVM Heap Size

Hi All

Thank you for reading my blogger.
Here i am trying to show how to read or get current jvm heap size. It is possible to read the default JVM heap size programmatic-ally 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);
}
}

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.


-Xms        set initial Java heap size
-Xmx        set maximum Java heap size
-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


 while running Tomcat under Eclipse for one of the web application I was getting Java Heap memory related error java.lang. Out Of Memory Error.
What needs to be done here basically is to increase the jvm heap size. So for increasing the JVM Heap Size of Tomcat in Eclipse we have to set few VM arguments of the tomcat.

Follow the simple steps to change the Heap Size of Tomcat under Eclipse.
1. Open the Server tab in Eclipse and double click the Tomcat server to open Server Configuration.


2. In Server Configuration, click on the Launch Configuration link under General Information.


3. Under Arguments tab, add following values in VM arguments.

-Xms64m -Xmx256m





1 comment:

ShivaShankarReddy Yannam said...

Very nice Reading This i got a New Point

Thx