Eclipse – java.lang.OutOfMemoryError: Java heap space

In Eclipse IDE, if your program is consuming a lot of memory (loading big data) like this :


  List<Domain> list = domainBo.findAllDomain(100000);
		
  for(Domain domain : list){
	process(domain.getDomainName());
  }

It can easily 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)

1. Solution – VM arguments

On Eclipse menu, clicks Run -> Run Configurations.., select the Java application you want to run, clicks on the Arguments tab, update the VM arguments with the following options


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

For example, -Xms512M -Xmx1024M

eclipse-out-of-memory

2. Mistake – eclipse.ini

The memory settings in eclipse.ini is allocated to Eclipse IDE only, not the program you want to run. A very common mistake is updated the heap size in eclipse.ini, and expects it to solve above out of memory problem.

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.

But, if your Eclipse IDE is always crashed by no reason, you can try to 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 eclipse.ini is located in the Eclipse installation folder.

References

  1. FAQ How do I increase the permgen size available to Eclipse?
  2. Find Out Your Java Heap Memory Size
  3. Eclipse + Tomcat – Java.Lang.OutOfMemoryError: Java Heap Space

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
baswaraj baswa
Guest
baswaraj baswa

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

sanjay paswan
Guest
sanjay paswan

That was very useful but if item to be added is huge and there is now to allocate more memory using -Xms and -Xmx . is there any way to catch heap error message.

Nonhlanhla Mthembu
Guest
Nonhlanhla Mthembu

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

Tami L
Guest
Tami L

Thank you!

Rupali
Guest
Rupali

Out Of Memory getting at raw hashmap use in Java 6, Tomcat 7;
private HashMap map= new HashMap();
Any idea.. thanks in advance..

Raj Kumar
Guest
Raj Kumar

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

pratiman tiwari
Guest
pratiman tiwari

Nice job..

yukti kaura
Guest
yukti kaura

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

hojun jung
Guest
hojun jung

Thank u for your discripting.

amitabh
Guest
amitabh

Thank you :)

Chris Miller
Guest
Chris Miller

Works Great Now! Thanks

Alinda
Guest
Alinda

Thanks again mkyong. Have a good day.

66
Guest
66

thanks’

Sean
Guest
Sean

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.

Carmen A
Guest
Carmen A

Excellent!

Akshay
Guest
Akshay

thanks for info