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


31 Comments

  • sarki 26 August, 2009, 21:13

    thank you

  • rajesh 1 September, 2009, 8:46

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

  • Anika 4 September, 2009, 23:26

    Thanks a lot
    -anika

  • pankaj 28 September, 2009, 13:38

    is there a way to set heap size permanently?

    • woody 5 October, 2012, 10:26

      in windows:
      (put control panel in icon view rather than category)
      control panel > java > java > view
      and add the -Xms and -Xmx to the runtime parameters.

  • sree 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 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 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 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.

  • Dhana 21 September, 2010, 21:48

    Hi All,

    How to check what is teh heap size in a windows machine in command line?

  • rohini 21 February, 2011, 15:58

    same error bt solution not working……!

  • Dharmvir 16 May, 2011, 23:19

    You can read about different parts of the heap on the following link:
    http://java-espresso.blogspot.com/2011/05/heap-structure-in-jvm.html
    Gist of the article is below:

    Following are the parts in JVM heap:

    Young Generation
    Eden Space
    Survivor space
    Virtual space
    Tenured (Old) generation:
    Virtual Space
    Other Areas: Not a part of heap:
    PermGen Area
    Native Area

  • rajesh.kolli 25 May, 2011, 13:54

    you can read different parts of the heap on the following link:

    http://java-espresso.blogspot.com/2011/05/heap-structure-in-jvm.html
    Gist of the article is below:

    Following are the parts in JVM heap:

    Young Generation
    Eden Space
    Survivor space
    Virtual space
    Tenured (Old) generation:
    Virtual Space
    Other Areas: Not a part of heap:
    PermGen Area
    Native Area

    http://viralpatel.net/blogs/jvm-java-increase-heap-size-setting-heap-size-jvm-heap/

    http://hausheer.osola.com/docs/5

    http://www.codemiles.com/java/here-how-to-increase-java-heap-size-t663.html

  • Dala 9 September, 2011, 1:44

    HI
    I tried to increase the java heap size I used this command line:
    java -Xmx1K
    I have this error :
    error occurred during initialization of VM
    could not reserve enough space for object heap
    could not create the java virtual machine
    I search and found the reason of this error is Heap size is larger than my computer’s physical memory , and I don’t know if it the real reason is the physical memory , can any one help my to fixed this error .

    • Leszek 7 January, 2013, 21:44

      java -Xmx1K -means (maybe -Xmx1k ) that maximal heap is.. 1kB, too less, try larger values like 20MB: -Xmx20m

  • Dala 9 September, 2011, 1:58

    I think it is not the real reason
    bye

  • Chris 20 September, 2011, 7:55

    There is a way to set this permanantly. In windows, go to control panel, click java, go to java tab, click view, and under runtime parameters put -Xmx4G or whatever size you want

    • snehal 16 May, 2012, 14:17

      And how to do this permanent heap size setting in Unix?

  • JigSaw 30 September, 2011, 0:21

    In Windows you should write it as;
    java -Xms64m -Xmx1024m -jar MyProgram.jar

    java -Xms64m -Xmx256m HelloWorld doesn’t work for me, I added -jar part and it works now.

  • Kellie B 10 October, 2011, 15:02

    I need to ‘increase java heap size in jvmcfg.ini’ I have no idea what this means but from the information above see I need to ‘Xmx set maximum Java heap size’, what does that mean I actually do? Cut and past that somewhere?

    I have a macbook and am not down with computer lingo so would really love some basic straightforward instruction for dummies.

  • Janaki 8 March, 2012, 16:02

    Can let know where to do the heap settings? exact path to do the settings.

    • Viral Patel 8 March, 2012, 16:18

      The heap settings are done as command line arguement for JVM (java command). You need to check where you want to give heap preferences. Check the JVM command (java ..) and pass these parameters accordingly.

  • Chaitanya 13 May, 2012, 22:56

    I have a spice qt-61.in it Java apps open too slowly can i do anything for it

  • swadhikar 21 November, 2012, 20:05

    Excellent illustration

  • Napril 23 November, 2012, 17:33

    Thanks a lot. Is there a way to set heap size permanently on macosx 10.7?

  • Hamid 6 March, 2013, 15:04

    Can I put any number like 1600m in Xmx or it has to be only figures like 32, 64, 128, 256, 1024, 2048 and so on. thanks

  • Pushpak Khatri 23 October, 2013, 2:29

    Dear sir

    i want to know that any software available that we can see that our variable store in which memory and which address? i want know that

    for ex: if i have addition of two variable so how can i see that which address and which memory store which variable? and at which address?

    so can u plz plz plz plz suggest me some of software that i can see how memory work on my program..

    Thanks

  • HarperS 14 March, 2014, 0:08

    You say: “The default heap size if 1 MB and can increase as much as 16 MB.” Are those accurate? If so, why is everybody referring to values that are larger, for example: -Xms64m -Xmx1024m, etc.?

  • Ankit SAhu 19 March, 2014, 19:37

    you can set java_opts in environment variable to set globally
    JAVA_OPTS=”-Xms1024m -Xmx2048m”

  • Deval 6 May, 2014, 13:30

    Dude…I really liked it

  • fool 19 May, 2014, 2:06

    Iam really confused here, maybe it’s stupid question but what does the HelloWorld means?
    I’ve got 32bit with 3gb ram, ive tried adding different parameters in control panel -> Java, also tried adding new environment variable JAVA_OPTS=… Still nothing, i want to try the commandline thing but can’t figure out what HelloWorld is…

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current day month ye@r *