Thursday, October 6, 2016

How to increase heap size of Eclipse - Solving OutOfMemoryError

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 heap size of Eclipse by using JVM memory options -Xms and -Xmx. There are two ways to provide JVM options to eclipse either updating 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 why I had to increase heap memory of Eclipse. I was getting "an internal error occurred during repository search . java heap space" while using Maven in Eclipse. Eclipse keep throwing java.lang.OutOfMemory:Java Heap Space while updating the index or searching for maven artifacts.

Before increasing heap size of Eclipse, first, check the current memory settings. You can do 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


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 following lines and choose how much Java heap space you want for Eclipse:


The first one is for starting memory and 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 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, 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 plugin or projects will certainly help to free some memory on Eclipse. You can further check Eclipse in Action to learn more about 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 Eclipse folder, next to eclispe.exe file:

Further Learning
The Eclipse Guided Tour - Part 1 and 2
Java Fundamentals, Part 1 and 2
The Java Developer's Guide to Eclipse

Other Eclipse tutorials and tips for Java developers
  • 3 Maven Eclipse Tips for Java Developers (tips)
  • How to remote debug Java application 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 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