-3

I am running Junit test case in Jenkins through build.xml. When I run a particular test case I get the below error.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 500 Java heap space</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /url. Reason:
<pre>    Java heap space</pre></p><h3>Caused by:</h3><pre>java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:410)
    at java.lang.Long.valueOf(Long.java:525)
    at org.codehaus.jettison.mapped.DefaultConverter.convertToJSONPrimitive(DefaultConverter.java:39)
    at org.codehaus.jettison.mapped.MappedNamespaceConvention.convertToJSONPrimitive(MappedNamespaceConvention.java:282)
    at org.codehaus.jettison.mapped.MappedXMLStreamWriter$JSONPropertyObject.withProperty(MappedXMLStreamWriter.java:153)
    at org.codehaus.jettison.mapped.MappedXMLStreamWriter$JSONProperty.withProperty(MappedXMLStreamWriter.java:66)
    at org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeEndElement(MappedXMLStreamWriter.java:247)
    at com.sun.xml.bind.v2.runtime.output.XMLStreamWriterOutput.endTag(XMLStreamWriterOutput.java:144)
    at com.sun.xml.bind.v2.runtime.output.XmlOutputAbstractImpl.endTag(XmlOutputAbstractImpl.java:120)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.leafElement(XMLSerializer.java:326)
    at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$StringImplImpl.writeLeafElement(RuntimeBuiltinLeafInfoImpl.java:1041)
    at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$StringImplImpl.writeLeafElement(RuntimeBuiltinLeafInfoImpl.java:1020)
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:252)
    at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:121)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
    at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:155)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:324)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)

Below is the build.xml code I use to run the junit testcase build.xml

<target name="runTests">
  <junit fork="yes" forkmode="once" printsummary="yes" dir="${basedir}" includeantruntime="yes" failureproperty="failureTest" maxmemory="1024m" jvm="">
  <jvmarg value="-XX:MaxPermSize=512M"/>
  <jvmarg value="-Dlog4j.configuration=log4j.properties"/>

But this error is occurring only in specific system, not in all the environments. How can I get rid of this?

trincot
  • 317,000
  • 35
  • 244
  • 286
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • what is the test that you are running? –  Feb 25 '13 at 14:29
  • 2
    This happens while building a response string. Considered that your response might be incorrectly generated and too large? – Thorbjørn Ravn Andersen Feb 25 '13 at 14:31
  • @Kata : I have lot of validate method in around 15 classes, this test will invoke all the methods and test will pass if there is no validation error returned. – Lolly Feb 25 '13 at 14:41
  • I think you append a lot of text to that StringBuilder, use a debugger and see where and what you append to it. – Christophe Roussy Feb 25 '13 at 14:44
  • @Thorbjørn Ravn Andersen: Response string will be null if there is no validation errors or else it return json array of objects which is a ValidationObject containing Message and Id. – Lolly Feb 25 '13 at 14:45
  • @annonymous well, there you have it. Look at your validation errors just before you build the response string. – Thorbjørn Ravn Andersen Feb 25 '13 at 15:20

3 Answers3

2

Without looking at the tests, it is really hard to help you but my guess is that whatever you are working with is large especially if it involves building strings. A few suggestions could help you:

  1. Try to use the StringBuilder class to build the response. I noticed that it makea a huge difference over using the "+" sign when building a large string.
  2. Increase the memory available to eclipse in the eclipse.ini. A good explanation can be found in this link.

I would try to inspect and change the code instead of changing the memory settings. As much as you can increase the memory disposed to the JVM, there is a reason why the initial memory settings are small. I think it is because developers should not just write memory run-away programs but should take care to architect programs that run on most environments, including the memory constrained ones.

Community
  • 1
  • 1
2

Starting from the stacktrace of an OOM is in most of the cases misleading, because once the heap gets almost full, any object allocation might be the one that fails.

Increasing the heap might help, but your usecase (testing some validations as you mentioned in a comment) sounds like it shouldn't require gigs of ram.

If you want a quick answer, use some tooling. Include the JVM arg: <jvmarg value="-XX:+HeapDumpOnOutOfMemoryError"/>, and once the heap is dumped open it in the Eclipse Memory Analyzer Tool to see what is eating up the memory. It's a very nice tool with which you can spot the problem in minutes (for the simpler cases).

One tip: if your test classes use the SpringJUnit4ClassRunner to load a test context, Spring will cache all contexts loaded during the run. If you have a dozen integration tests with different contexts, you have a good chance of running out of that 512m heap.

zagyi
  • 17,223
  • 4
  • 51
  • 48
0

Please try increasing your heapspace?

chiru
  • 812
  • 5
  • 17
  • 32