Increasing Heap Size in Eclipse

Ran into the problem of running out of heap space when running my program on Eclipse the other day. It just so happens that the heap space that is allocated for my programs in Eclipse on my Mac at home is less than what is supplied for my work laptop, thus making my program crash at home and not on my laptop. When dealing with a huge amount of data and objects in a large hash array of trees, heap space can run out pretty quick. So, after digging around a bit on Google I found two simple solutions that I continued to run into. Click here or the Read More link for solutions…

1) Changing Eclipse’s .ini file: This file could be found in the same folder as your Eclipse executable file. For Mac users just right click the executable file and choose the Show Package Contents option. This will allow you to locate the .ini file. Opening the .ini file you will see some options like this:

-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vmargs
-Xms40m
-Xmx256m

What this is showing is that the minimum heap space (-Xms40m) is set to 40MB and the maximum heap space (-Xmx256m) is set to 256MB. Changing the 256m to something bigger like 512m should increase the heap space Eclipse supplies. When I opened my .ini file it was actually showing a max heap space larger than what was showing in my program. Note: to check your max available heap space in Java use this line of code:

System.out.println(java.lang.Runtime.getRuntime().maxMemory()); 

Eclipse was actually not limiting my heap space because my .ini file showed a max of 512MB while my program reported only 128MB. This then brought me to try the second method of fixing this problem…

2) Inserting VM arguments into your JRE: while Eclipse is open, open up Preferences (for Windows: located under Window menu item. for Mac: located under Eclipse menu item), open the Java section, click on Installed JREs, click on the JRE being used (probably will be jre6), click on the Edit… button, and insert this into the Default VM Arguments:

-Xms256M -Xmx512M

The format is similar to what is in the .ini file where Xms is minimum space and Xmx is maximum space. The problem was that the JVM was restricting the amount of memory available on the heap so arguments had to be passed to the VM to alter this. These commands can be done through command line:

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

For further information on this topic, here is the official Eclipse link to where I found my information.