Friday, April 23, 2010

Memory Errors / "Java heap space error"

While using the Eclipse environment, it is particularly easy to run out of room; if you've got a decent machine, it is easy to assume that the available RAM on the machine will automatically transfer to available build ram. This is not the case; thus, you may occasionally get errors that state Java heap space error or an Error that offers to close Eclipse.

Documentation exists in various locations; thus this post is intended to capture the notes I found useful from various sites. Where applicable I intend to link to the location I found the information (in case some further piece of information is of value to you).

=================================================

Setting the java heap size is fairly simple - it can be done in a number places.
The first (and potentially most obvious) is via command line; simply type them out.
example: java -Xmx128m
I am not terribly familiar with the command line interface for Eclipse, so I won't be documenting particulars; below, however, are some guidelines about how to set the parameters and what to watch out for when asserting them.

The second, and more obvious to me, is through the use of the initialization file, entitled "eclipse.ini". It is located in the directory in which you installed eclipse ... the one that has the "dropins" folder. Opening it up reveals several settings - the one you will be interested in is under "-vmargs".

The third is through the Eclipse interface proper.
I snagged the steps from here.

1. From the Eclipse main menu, choose "Window" then "Preferences".

2. On the Preferences dialog
Expand "Java" in the left-hand column
Under Java, click "Installed JREs"
You will now see your installed JREs with the one currently used being ticked
Select the "default" JRE and click the "Edit" button to the right of it.

3. Part way down the "Edit JRE" dialog, you'll see a field labeled "Default VM Arguments".
Go to this and put in your -X memory arguments.
I used "-Xms128M -Xmx512M".

Regardless of your preferred method, the two attributes are "-Xms80m" and "-Xmx256".
Xms refers to the start heap size, Xmx to the max heap size.

You need enough initial heap size to start building and you need enough maximum heap size to allow Eclipse/java to finish whatever tasks you've intended it to complete.

Several potential errors (link) in setting them include
  • missing units or inappropriate units
  • extra spaces, incorrect syntax, or non-whole units when assigning the amount of memory
  • assigning unreasonable memory sizes
Missing / inappropriate units
examples of missing a unit: "-Xms42" or "-Xmx100"
valid options for units are "m" or "g"; "mb" and "gb" are wrong
units are case insensitive; thus "m", "M", "g", and "G" are all valid units
Extra spaces, incorrect syntax, or non-whole units when assigning the amount of memory
parsing of the command requires no spacing or operator (like an equals sign)
correct ...  "-Xms75m"
incorrect ..."-Xms 75m"; "-Xms=75m"; "-Xms:75m"
in addition, separation of separate commands (like setting Xms and Xmx) is simple
if using the .ini, place each on a separate line
if setting the Xms and Xmx within Eclipse, put on the same line, one space apart
Ensure the amount of memory that you set aside is a whole number
correct ... -Xms101m
incorrect ... -Xms1.3m

Assigning unreasonable memory requests
Note: The default heap range is from 0 to 64 mb.
Setting the Xms in excess of 64 mb will likely net you:
Incompatible initial and maximum heap sizes specified
Setting either Xms or Xmx in excess of your computer's available RAM will show
Could not reserve enough space for object heap
Could not create the Java virtual machine
Setting either Xms or Xmx in excess of current anticipated use
-Xmx1g is reasonable (though high)
-Xmx512g is not reasonable

As you can see, setting the initial and maximum heap sizes is simple.  This is also implicit as most of this post was about how to properly format the values to which you set requested memory.  If you find anything wrong with these directions or think a particular aspect of clarity should be included, please comment.


Thanks.

No comments:

Post a Comment