Playing with JVM / Java Heap Size.

      

java-logo
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.

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

Facebook  Twitter      Stumbleupon  Delicious
  

8 Comments on “Playing with JVM / Java Heap Size.”

  • sarki wrote on 26 August, 2009, 21:13

    thank you

  • rajesh wrote on 1 September, 2009, 8:46

    to set hep size in tomcat http://hiox.org/index.php?id=443

  • Anika wrote on 4 September, 2009, 23:26

    Thanks a lot
    -anika

  • pankaj wrote on 28 September, 2009, 13:38

    is there a way to set heap size permanently?

  • sree wrote on 5 November, 2009, 8:04

    No. There is no way to set heap size permanently. But we use eclipse just pass the arguments like -Xmx ..

  • vamsee wrote on 3 March, 2010, 17:55

    one simple way is to write a bat file to set permanently:

    we can write a bat file which opens the required jar file with proper heap size settings. For example :
    java -Xms64m -Xmx256m HelloWorld

    this can be put in a bat file.

  • pol wrote on 3 August, 2010, 2:12

    Im doing the bat file but I have a problem I have the .exe file… not the class
    I any wat to do it whit the .bat?
    or another way to set it?
    I don;t know if its possible in java code to change it

  • rafikpbhdm wrote on 16 August, 2010, 18:03

    hi my friends, well I tried to increase java heap space using run configuration in eclipse and netbeans, and the result was same ,I had NoClassDefFoundError , and when I deleted the command line (java -Xms64m -Xmx256m) and run the application again it worked normally except of course the pb of OutOfMemory exception, and even try dos prompt ,the vm didn’t recognize the option -Xmx and sometimes it just throw the excption NoClassDefFoundError,well it ‘s really weird thing and I don’t know how to explain it ,so little help will be nice.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.