java.lang.OutOfMemoryError: Java heap space : Cause and Solution

So you are getting java.lang.OutOfMemoryError: Java heap space and run out of ideas on what to do, especially if you are a user of any Java application and not the programmer or developer, this could be a tricky situation to be in. I receive lots of emails from Minecraft user ( a popular Java game), along with junior developers who are using Tomcat, Jetty, Untow, JBoss, WebSphere, Android user, who uses Android apps and several other Swing-based Java desktop application user complaining about java.lang.OutOfMemoryError: Java heap space in their Mobile or Laptop. 

For users, there is not much they can do, but to restart the application. When you restart your application, previous memory get reclaimed by OS and it's a fresh start for your application, but you may lose data, which is not saved, especially if you are playing Java games like Minecraft.

Junior programmers, they have some options, but they need to understand that all these web and application servers and IDEs like Eclipse, IntelliJ, and NetBeans are Java programs and can be configured to run with more memory.  

You just need to find out their startup script, from where they are launching the java command to start the process and modify the value of -Xmx JVM option to a higher value, subject to your machine configuration, or simply add -Xmx JVM option if it's not already there.

Before doing this it's better to understand some basics about Java Heap memory, why java.lang.OutOfMemoryError: Java heap space comes and what are JVM options to fix this error.

Basics about Java Heap Memory 

When we start the Java program, JVM requests some memory from OS. Based upon whether your Java Program is running on Windows, Linux, Solaris, or Android-based smartphones, there is a default Java Heap space and a maximum Java Heap memory.

Theoretically in a 32-bit machine, the maximum memory a process can have is in the range of 2^32 i.e 4GB, but in reality, the actual limit is quite less e.g. somewhere around 1.7GB in Windows and around 2.5 GB in Linux. I suggest reading this article to know more about these limits.

Similarly, you can either run on a 32-bit JVM or 64-bit JVM, if your machine is a 64-bit machine, then this maximum heap space limit extended to a very high value of 2^64 bit, but again limited by your physical RAM. Now a brief about this error, by reading java.lang.OutOfMemoryError: Java heap space, we can clearly see that JVM is running out of memory in heap space.

Heap space is where objects are created in Java, and as your play games, deploy applications or web servers like Tomcat, JBoss, and Websphere, lots of objects are created in Heap memory. Later, when those objects become eligible for Garbage collection, they are collected by Garbage Collector and memory is returned to Java Heap Space.

If there are too many live objects in memory, because of poor programming or if memory is not enough to hold a required number of objects, you will get java.lang.OutOfMemoryError: Java heap space, when Java tries to create another object.

How to fix java.lang.OutOfMemoryError: Java heap space

JVM Option to fix java.lang.OutOfMemoryError: Java Heap Space

java.lang.OutOfMemoryError: Java heap Space in Eclipse, Tomcat, MineCraftNow there are 2 ways to fix java.lang.OutOfMemoryError: Java heap space, either by finding any memory leak or by increasing heap space. There are two JVM options, which is particularly important to java.lang.OutOfMemoryError: Java heap space, is -Xms and -Xmx, which configures starting and maximum heap memory for JVM.

-Xms<size> to set initial Java heap size like -Xms1024M setting 1GB -Xmx<size> to set maximum Java heap size e.g. -Xmx2048M setting 2GB

These JVM options are provided at the time of starting the Java application. If you are getting java.lang.OutOfMemoryError: Java heap space in Eclipse, Netbeans, IntelliJ, Tomcat, JBoss, or WebSphere, where you don't have any control on source code, you can simply find their start-up script and can increase the value of -Xmx parameter.

For Eclipse this data is in eclipse.ini file in vmargs section.

For example on my computer, Eclipse has the following memory settings :

$ vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx384m.

Similarly, Tomcat keeps its Java heap settings on catalina.bat or depending upon, whether you are running Tomcat on Windows or UNIX.

Similarly, for all other Java applications, you just need to find the file, where these settings are located or from where "java" command is executed. Increasing Java heap space can immediately solve this problem, but if you see this problem again, then there is a good chance of memory leak in that Java application.

Two words on Memory Leak in Java Application

Saying that a Java program can have a memory leak is a little surprising for many programmers because they think that Garbage Collector will reclaim memory, and that's one of the reasons, why Java is a popular programming language. Bug, GC has its limitation, it can not reclaim the object, which is undesired but still referenced by some object in Program.

This memory, which should be free but still can not be reclaimed is called Memory leak in Java. If you see java.lang.OutOfMemoryError: Java heap space, even after increasing heap space, then it's good to analyze memory pattern for any possible memory leak.

There are tools like Profiler (JProbe, Yourkit, or Netbeans Profiler), JConsole, VisualVM, which you can use to see the memory usage of Java applications to confirm memory leaks.

There are even tools like Plumbr, which can help you to find memory leaks in Java. Though this would be as easy as increasing heap memory to fix java.lang.OutOfMemoryError: Java heap space.


  1. Excellent write-up, especially a beginner like me, who doesn't have much knowledge of Heap space, memory leak and the java.lang.OutOfMemoryError itself. I was looking for JVM flags to provide more memory but glad to know some basic details, which is really hard to come by.

  2. First of all, thank you for giving us credit. But maybe you can fix a typo, cause our brand we ship our cure for memory leaks is Plumbr, not Plumber as currently in the post.

    Ivo, from the Plumbr team.

    1. Hello Ivo, thanks to dropping by. Sorry for typo, looks like spell checker :) fixed now.

  3. Out Of memory error is a runtime blunders in Java which occurs while the Java digital system (JVM) is not able to allocate an item because of inadequate area inside the Java heap. This error can also be thrown when the local memory is inadequate to guide the loading of a Java magnificence.


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