Main Tutorials

Eclipse – java.lang.OutOfMemoryError: Java heap space

This article shows how to solve the java.lang.OutOfMemoryError: Java heap space in Eclipse IDE.

Table of contents

1. Eclipse – OutOfMemoryError: Java heap space

In Eclipse IDE, if the Java code is consuming a lot of memory (loading big data at once time) like this :


  // loads a lot of data
  List<Domain> list = domainBo.findAllDomain(100000);

  for(Domain domain : list){
    process(domain.getDomainName());
  }

We hit java.lang.OutOfMemoryError: Java heap space :


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  at java.util.HashMap.<init>(HashMap.java:209)
  at java.util.LinkedHashMap.<init>(LinkedHashMap.java:181)

2. Temporary fix – Increase the heap size

On the Eclipse menu, clicks Run -> Run Configurations.., select the Java application we want to run, click on the Arguments tab, VM arguments section, and adjust a better Java initial maximum heap size.

Terminal

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size

For example, -Xms512M -Xmx1024M

Eclipse VM arguments

3. eclipse.ini

The memory settings in eclipse.ini are allocated to Eclipse IDE only, not the program we want to run. A common mistake is updated the heap size in eclipse.ini, and expects it will solve the heap size error in the program.

But, if no reason constantly crashes the Eclipse IDE, we can increase the heap size and perm gen in eclipse.ini.

/Users/mkyong/Downloads/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini

  -startu
  openFile
  -showsplash
  //...
  -XX:MaxPermSize=512m
  -Xms512m
  -Xmx1024m
  //...
  -Xdock:icon=../Resources/Eclipse.icns
  -XstartOnFirstThread

P.S Find eclipse.ini in the Eclipse installation folder.

Note
The Java application, Ant / Maven build scripts, or unit test cases, are run as an external tool from Eclipse, and it does not inherit the VM settings in eclipse.ini.

4. The solution, find the reason behind the heap size error.

We can increase the Java heap size to divert the problem temporarily, and the same heap size problem will come back to us later. The real solution is to determine the root cause of the Java heap size error and why we need so much heap size?

In the above code, why we need to load 100k of records at once time? Can we stream the data or process the data by batch?


  // Why? Can you stream it? Or process by batch?
  List<Domain> list = domainBo.findAllDomain(100000);

  for(Domain domain : list){
    process(domain.getDomainName());
  }

If we are not sure what is the root caused, try a profiling tool like Java Flight Recorder to dumps and analyze the JVM.

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nonhlanhla Mthembu
7 years ago

java.lang.OutOfMemoryError: GC overhead limit exceeded. I am using eclipse + boss EAP 6.1 + Spring 4 and Java 8. Thanking you in advance

baswaraj baswa
8 years ago

OutOfMemoryError heap space while huge data adding in excel used jdk7,and tomcat 8,luna4.2 eclipse, thanks advance.

Ramesh
4 years ago

Thanks indeed, this saved my day!

Swapnil Kute
4 years ago

java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)

in my condition both solutions are not working

Raj Kumar
8 years ago

Hi, how to fix application memory leakage issues ?
any ideas with examples. Thank you.

MikeB
4 years ago
Reply to  Raj Kumar

Generally, GC takes care of it, but if you want to optimize your app make sure to kill graphically intense objects right after use.

pratiman tiwari
8 years ago

Nice job..

amitabh
9 years ago

Thank you 🙂

Sean
10 years ago

Parameter which I always use -vmargs -Xms128m -Xmx384m -XX:MaxPermSize=384m but I not set inside eclispe but is create a batch file to call eclipse.exe.

yukti kaura
8 years ago

You are a saviour.Keep up the excellent work!!!