OutOfMemory/Heap space errors in Java

Recently I had to work on a Java based rule modeling tool that was built on Java/Eclipse. The tool was pushing both eclipse and the JVM to the limits in terms of memory and I was getting a lot of OutOfMemory exceptions and out of heap space errors, usually these can be fixed by increasing the default limits.

<p>
  The important settings in question are arguments passed to the JVM on startup, <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html">http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html</a>
</p></p> 

<p>
  <b><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">-Xms<i>n</i></span></b>
</p>

<p style="margin-left: .5in;">
  <span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">Specify the initial size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 1MB. Append the letter<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">k</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">K</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">to indicate kilobytes, or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">m</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">M</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">to indicate megabytes. The default value is chosen at runtime based on system configuration. For more information, see<span class="apple-converted-space">&nbsp;</span><a href="http://java.sun.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html"><span style="color: black;">HotSpot Ergonomics</span></a><span class="apple-converted-space">&nbsp;</span><br /> Examples:</span>
</p>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xms6291456</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xms6144k</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xms6m</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></pre>

<p>
  <b><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">-Xmx<i>n</i></span></b>
</p>

<p style="margin-left: .5in;">
  <span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">k</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">K</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">to indicate kilobytes, or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">m</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">or<span class="apple-converted-space">&nbsp;</span></span><tt><span style="font-size: 10.0pt; font-family: Courier; color: #444444;">M</span></tt><span class="apple-converted-space"><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">&nbsp;</span></span><span style="font-size: 9.0pt; font-family: Arial,sans-serif; color: black;">to indicate megabytes. The default value is chosen at runtime based on system configuration. For more information, see<span class="apple-converted-space">&nbsp;</span><a href="http://java.sun.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html"><span style="color: black;">HotSpot Ergonomics</span></a><span class="apple-converted-space">&nbsp;</span><br /> Examples:</span>
</p>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xmx83886080</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xmx81920k</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Xmx80m</span></pre>

<pre style="margin-left: .5in;"><span style="font-family: Courier; color: #444444;"></span></pre>

<p>
  Other advanced VM settings are documented here, <a href="http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp">http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp</a>
</p></p> 

<p class="MsoListParagraph" style="">
  <span style="">1.<span style="font: 7.0pt Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>Eclipse IDE - If the IDE itself is running out of memory when compiling or editing code then you can increase the maximum memory pool using the Xmx option in eclipse.ini file present in the same folder as eclipse.exe.
</p>

<p class="MsoListParagraph">
  <p class="MsoListParagraph" style="">
    <span style="">2.<span style="font: 7.0pt Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>JVM from within eclipse - If the JVM is running out of memory when running an app from within eclipse, then you can adjust the memory settings from the JVM configuration tab within eclipse.
  </p>
  
  <p class="MsoListParagraph">
    <p class="MsoListParagraph" style="">
      <span style="">3.<span style="font: 7.0pt Times New Roman;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>JVM from Tomcat - On the Apache Tomcat configuration utility on the Java tab, you can specify the memory and heap settings.
    </p>
    
    <p class="MsoListParagraph">
    </p></div> 
    
    <p style="font-size: 10px;">
      <a href="http://posterous.com">Posted via email</a> from <a href="http://sijin.posterous.com/outofmemoryheap-space-errors-in-java">Sijin Joseph</a>
    </p>
  </p></div>
Sijin Joseph
Sijin Joseph

Hands-on technology leader with 15+ yrs of experience in launching products, scaling up teams and setting up software infrastructure.