Sunday, January 14, 2024

How to Increase Heap Size of Java Program running in Eclipse [Example]

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 the 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 the maximum heap size of a 64-bit JVM.

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

$ 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 an 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 issues. 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.




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 a step by step guide to set  the heap size of the 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 the 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 the operating system you are running you may get the Invalid maximum heap size error as I got when I tried to set the 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 the heap size of Java applications running on Eclipse as well as on the command line. Expanding the heap size of a Java program in Eclipse is a straightforward yet pivotal step to enhance the application's performance, especially when dealing with large datasets or resource-intensive tasks. 

This guide has demonstrated a clear example of how to modify the heap size settings, providing developers with the flexibility to optimize memory allocation based on their application's requirements. 

By navigating through Eclipse's configuration parameters, developers can fine-tune their Java programs, ensuring efficient memory utilization and mitigating potential OutOfMemory errors. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.