Wednesday, May 17, 2023

How to increase heap size of Eclipse - Solving OutOfMemoryError? Example

If you are running lots of Java projects in Eclipse and it's throwing OutOfMemoryError every now and then it's time to increase the heap memory of Eclipse. Since Eclipse is a Java program, you can increase the heap size of Eclipse by using JVM memory options -Xms and -Xmx. There are two ways to provide JVM options to eclipse either updating the Eclipse shortcut or adding -vmargs on eclipse.ini file. I prefer the second option because it's clean. I'll tell you the exact steps to increase the java heap space in Eclipse but before that some background on why I had to increase the heap memory of Eclipse.

I was getting "an internal error occurred during repository search. java heap space" while using Maven in Eclipse. Eclipse keeps throwing java.lang.OutOfMemory: Java Heap Space while updating the index or searching for maven artifacts.

Before increasing the heap size of Eclipse, first, check the current memory settings. You can do it by opening the eclipse.ini file from the eclipse installation folder, next to the eclipse.exe file.

Here is what I had in my eclipse.ini file:
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20140116-2212
-product
org.eclipse.epp.package.standard.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx512m
You can see I was already running with 512M but even that is not sufficient for Maven :-(. Actually, the additional memory might be due to multiple nexus repositories configured on my Maven settings.xml file. Anyway, it seems time to increase memory a few more MB.



If you don't see the -vmargs in your eclipse.ini, just add the following lines and choose how much Java heap space you want for Eclipse:
-vmargs
-Xms40m
-Xmx512m

The first one is for starting memory and the second argument -Xmx is to maximum memory, you can use suffix k (kilobytes), m (megabytes), and g (gigabytes), but beware of space, otherwise, you will get invalid heap size error when you start the eclipse.

The second way to increase heap size is just to pass the -vmargs directly to the eclipse.exe file if you are starting it from a desktop shortcut or a startup script as shown below:

eclipse [normal arguments] -vmargs -Xmx512m [more VM args]

Now, the maximum Java heap space for eclipse is set to 512 megabytes. If you still see OutOfMemoryError in Eclipse, you probably need to investigate more which plugin/feature or project is creating the problem. Disabling some plugins or projects will certainly help to free some memory on Eclipse. You can further check Eclipse in Action book to learn more about the different features of Eclipse and how to fine-tune it for Java development.

Here is the screenshot which shows where you can find the eclipse.ini file for increasing the heap memory for Eclipse, it's inside the Eclipse folder, next to eclispe.exe file:



Other Eclipse tutorials and tips for Java developers
  • 3 Maven Eclipse Tips for Java Developers (tips)
  • How to remote debug Java applications in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developer should know? (see here)
  • How to add or remove External JARs from Eclipse Classpath? (tutorial)
  • How to attach source code for JAR file in Eclipse? (guide)
  • Eclipse short to print System.out.println statements? (shortcut)
  • How to increase the console buffer size in Eclipse? (steps)
  • How to fix Maven repository search not working in Eclipse? (solution)
  • How to use spaces instead of tabs in Eclipse? (guide)

No comments:

Post a Comment