1

I am using JAXB to marshal and unmarshal java object to xml and vise versa. The object I am marshaling has a byte array property. If the byte array has large size I got OOM.

Here is the marshaling I am doing:

StringWriter writer = new StringWriter();
jaxbContext = JAXBContext.newInstance("package name");
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
jaxbMarshaller.marshal(myObject, writer);
return writer.toString();

the error I am getting on the line:

jaxbMarshaller.marshal(myObject, writer);

Here is the stack strace:

java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuffer.append(Unknown Source)
    at java.io.StringWriter.write(Unknown Source)
    at java.io.BufferedWriter.flushBuffer(Unknown Source)
    at java.io.BufferedWriter.flush(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.cleanUp(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)

Here is the java memory statistics :

 uintx AdaptivePermSizeWeight                    = 20              {product}
 intx CompilerThreadStackSize                   = 0               {pd product}
 uintx ErgoHeapSizeLimit                         = 0               {product}
 uintx HeapSizePerGCThread                       = 87241520        {product}
 uintx InitialHeapSize                          := 90006400        {product}
 uintx LargePageHeapSizeThreshold                = 134217728       {product}
 uintx MaxHeapSize                              := 1440743424      {product}
 uintx MaxPermSize                               = 85983232        {pd product}
 uintx PermSize                                  = 21757952        {pd product}
 intx ThreadStackSize                           = 0               {pd product}
 intx VMThreadStackSize                         = 0               {pd product}

any help is greatly appreciated.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
  • What are you current heap and memory settings? Are you running an application or a web app? – Tim Biegeleisen Oct 12 '15 at 05:38
  • @Tim Biegeleisen can you tell me how to check current heap and memory setting.. and it's a web app – Shekhar Khairnar Oct 12 '15 at 05:48
  • Have a look [here](http://stackoverflow.com/questions/12688778/increase-tomcat-memory-settings), which discusses how to do this. Also have a look [here](http://wiki.razuna.com/display/ecp/Adjusting+Memory+Settings+for+Tomcat). – Tim Biegeleisen Oct 12 '15 at 05:53
  • 1
    Marshalling a presumably big myObject into an in-memory String may cause OOM. Why don't you marshal to a file? – laune Oct 12 '15 at 06:50
  • @ShekharKhairnar what do you expect from this post - what is your question? If you expect how to quickly fix the problem, then the answear would be expanding JVM memory. If you want to know the root cause of this problem, then probably you should read the sources of JAXB, but I think you could stumble upon bug that I found http://stackoverflow.com/questions/31927533/why-cxf-jaxb-read-whole-inputstream-into-memory-before-marshalling-to-soap-mes – ljader Oct 16 '15 at 07:45

3 Answers3

1

Alternatively you can use streaming mode while marshalling your JAXB. Please refer the below post for details:

How to stream large Files using JAXB Marshaller?

This approach would prevent your app from loading huge data in StringWriter object which causes this OutOfMemoryError

Community
  • 1
  • 1
Prem
  • 157
  • 7
0

Please use Java command line parameters

-Xms: initial heap size
-Xmx: Maximum heap size

if you are using Eclipse IDE. Please check in the following link

http://stackoverflow.com/questions/2294268/how-can-i-increase-the-jvm-memory

Good Luck

T8Z
  • 691
  • 7
  • 17
-1

You can use the following vm args to increase the jvm memory.

-vmargs
-Xmx512m
-XX:MaxPermSize=384m

Again the values to be specified are based on size of your RAM memory. The above values are optimal for a machine with 3GB RAM / 32 bit

These vm args need to be passed as command line arguments to your java application. If you are using eclipse then specify in your run configuration.

Prem
  • 157
  • 7