How to Increasing Heap Size in Java application or JVM?

Hello guys, if you are wondering how to change the heap size of the heap space of your Java application then you have come to the right place. In this article, I am going to tell you how to increase Java heap space so that your JVM will not crash using OutOfmemoryError. We have already seen how to increase heap memory in Maven and ANT and now we will learn how to increase heap size in Java, Eclipse, Tomcat, and WebSphere Server in a series of articles. Since all these are Java applications, once you know how to change heap space in Java, you can do that in any Java application, provided you know the right place, which is what we will see in this article.

How to increase Heap space in Java Virtual Machine

I am working on a Windows 2019 server (64-bit) with 8 GB ram. How can I increase the heap memory maximum? I am using the -Xmx1500m flag to increase the heap size to 1500 Mb. Can I increase the heap memory to 75% of physical memory (6 GB Heap)?

Actually, you can increase Java heap space up to 4GB on a 32-bit system, I mean on a 32-bit Java Virtual Machine.

If you're on a 64-bit system and 64-bit Java virtual machine then you can go higher. No need to worry if you've chosen incorrectly, if you ask for 5g on a 32-bit system java will complain about an invalid heap space error and quit.

Now, the question comes, how can you change the Java heap space? Well, you can use the cmd-line flags - like

$ java -Xmx6g myprogram

You can get a full list (or a nearly full list, anyway) by typing java -X.

It is possible to increase the heap size allocated by the JVM by using command-line options

Here we have 3 options

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

For example

$ java -Xms16m -Xmx64m ClassName

In the above line, we can set the minimum heap to 16mb and maximum heap 64mb. You can further check a comprehensive course like Java Application Performance and Management on Udemy to learn more about how memory works in Java application.

How to Increasing Heap Size in Java application or JVM?

On a 32-bit JVM, the largest heap size you can theoretically set is 4gb but practically you won't be able to set that much. It somewhere around 3 to 3.5 GB depending upon which operating system you are running like Linux, Windows, Mac OS X, and other variations of Linux.

To use a larger heap size, you need to use a 64-bit JVM. Try the following:

$ java -Xmx6144M -d64

The -d64 flag is important as this tells the JVM to run in 64-bit mode.

That's all about how to increase the heap size of the Java application. Ultimately, just knowing the two JVM parameters for initial and maximum heap space is enough.  If you know those parameters then you can change the heap size of the Java application.

It's also good to remember the practical limitations, especially around 32-GB and the impact of the larger heap, likey a longer pause time during GC to make a conscious choice. That's why I highly recommend you to join a good course on Java performance like Java Application Performance and Management on Udemy to learn more about this important topic.

Further Learning
Java Memory Management (Udemy)
Understanding Java Virtual Machine (Pluralsight)
The Definitive Guide of Java Performance (book)

Other Programming Resource articles you may like to explore

Thanks for reading this article so far. If you like small Java tutorials to increase the heap size of JVM then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are new to the Java world and looking for some free online courses to start learning Java programming language then you can also, check out Java Programming: Beginner to Guru - A free course on Udemy to start your Java developer journey.

No comments:

Post a Comment