How to Increase Heap Size of Java Program running in Eclipse

It is possible to increase the heap size allocated by the JVM by using command line options Here we have 3 options

-Xms<size>        to set the initial Java heap size
-Xmx<size>        to set the maximum Java heap size
-Xss<size>          to set the java thread stack size

$ java -Xms1G -Xmx2G MainClass

In the above line, we have set the minimum heap size to 1GB and maximum heap size to 2GB.


On a 32-bit JVM, the largest heap size you can theoretically set is 4GB, but in practice, you will never see that level. Practically, you will get around 1.5GB in 32-bit Windows, 2.5GB on 32 bit Linux and slightly more on 32-bit Solaris system.  To use a larger heap size, you need to use a 64-bit JVM, where you can theoretically set quite a large heap space. See this article for learning more about maximum heap size of a 64-bit JVM.

In Solaris, you can set large heap space on a 64-bit JVM as following:

$ java -Xmx6144M -d64 MainClass

The -d64 flag is important as this tells the JVM to run in 64-bit mode. See here to learn about some more important JVM arguments, every Java developer should know.


If you are experienced Java developer then it's also expected from you to know more about JVM, Garbage collection and how to tune both JVM and Garbage collection. You should also know about tools like a profiler, heap dump analyzer to troubleshooting application performance issue. If you want to improve your JVM and GC tuning skill then you should check out Java Performance The Definitive Guide By Scott Oaks, one of the must read a book for experienced Java programmers.

How to set memory of Java application in Eclipse



Increasing Heap Memory for Java Program Running on Eclipse

It is possible to increase heap size allocated by the JVM in eclipse directly In eclipse IDE go to

Run---->Run Configurations---->Chose the right application --->VM Arguments

Enter -Xmx1G, to set the max size to 1GB. You can also use M or K, which stands for Megabytes and Kilobytes for example -Xmx256M means maximum heap space would be 256 Megabytes.

Here is step by step guide to set  the heap size of Java program in Eclipse:

Step 1: Go to the Run Configuration of Application or Program for which you want to increase the heap space.

How to set heap size of Java application in Eclipse


Here we are increasing heap size of MultipleThreadDemo application which is run by the Main class MultipleThreadDemo, as you can see in configuration.

Step 2: Enter correct JVM options at the VM Arguments area

How to increase heap size in Eclipse


Beware not to add the JVM arguments at the Program Arguments text area, that's for program arguments e.g anything you want to use to program using -D e.g. -Dversion="1.0"



Important points

1) Depending upon the heap space you provide and operating system you are running you may get the Invalid maximum heap size error as I got when I tried to set maximum heap size for a 32-bit JVM in Windows to 1.5G:

Invalid maximum heap size: -Xmx1.5G

When you run the program you will also see the "Error: Could not create the Java Virtual Machine" as seen in the following pop-up which indicates the JVM is not created due to invalid heap size:

How to set Java Heap space in Eclipse


2. Make sure there is no space between -Xmx and 1G e.g. -Xmx 1G will also throw invalid heap size error.

3. One more common mistake you need to avoid is using suffix like GB, MB, and KB instead of G, M, and K, those will also result in could not create the Java Virtual Machine error.


That's all about how to increase heap size of Java application running on the Eclipse as well as on the command line.



No comments:

Post a Comment