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
- 2. Temporary fix – Increase the heap size
- 3. eclipse.ini
- 4. The solution, find the reason behind the heap size error.
- 5. References
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.
-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size
For example, -Xms512M -Xmx1024M
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
.
-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.
java.lang.OutOfMemoryError: GC overhead limit exceeded. I am using eclipse + boss EAP 6.1 + Spring 4 and Java 8. Thanking you in advance
OutOfMemoryError heap space while huge data adding in excel used jdk7,and tomcat 8,luna4.2 eclipse, thanks advance.
Thanks indeed, this saved my day!
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
in my condition both solutions are not working
How much of data you are copying? Can you stream it?
Hi, how to fix application memory leakage issues ?
any ideas with examples. Thank you.
Generally, GC takes care of it, but if you want to optimize your app make sure to kill graphically intense objects right after use.
Nice job..
Thank you 🙂
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.
You are a saviour.Keep up the excellent work!!!