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 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, 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 there 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.

For Junior programmers, they have some options, but hey need to understand that all this web and application server 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 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 option to fix this error.

Basics about Java Heap Memory 

java.lang.OutOfMemoryError: Java heap Space in Eclipse, Tomcat, MineCraftWhen we start Java program, JVM request some memory from OS. Based upon whether your Java Program is running on Windows, Linux, Solaris or Android based smartphone, there is a default Java Heap space and a maximum Java Heap memory. 

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

Similarly you can either run on a 32-bit JVM or 64-bit JVM, if your machine is 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 ran 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 object 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 required number of object, 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

Now 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 e.g. -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 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 there start-up script and can increase value of -Xmx parameter.

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

For example on my computer, Eclipse has following memory settings :
vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx384m.

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

Similarly for all other Java application you just need to find the file, where this 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 good chance of memory leak in that Java application.

Two words on Memory Leak in Java Application

Saying that a Java program can have memory leak is little surprising for many programers, because they think that Garbage Collector will reclaim memory, and that's one of the reason, why Java is popular programming language. Bug, GC has it's limitation, it can not reclaim the object, which is undesired but still refrenced 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, than 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 memory usage of Java application to confirm memory leak. 


  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.


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