我正在使用JAXB来编组和解组java对象到xml,反之亦然。 我正在编组的对象有一个字节数组属性。如果字节数组的大小我得到了OOM。
这是我正在做的编组:
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();
我上线的错误:
jaxbMarshaller.marshal(myObject, writer);
这是堆栈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)
这是java内存统计信息:
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}
非常感谢任何帮助。
答案 0 :(得分:1)
或者,您可以在编组JAXB时使用流模式。有关详细信息,请参阅以下帖子:
How to stream large Files using JAXB Marshaller?
此方法会阻止您的应用在StringWriter对象中加载大量数据,从而导致此OutOfMemoryError
答案 1 :(得分:0)
请使用Java命令行参数
-Xms: initial heap size
-Xmx: Maximum heap size
如果您使用的是Eclipse IDE。请检查以下链接
http://stackoverflow.com/questions/2294268/how-can-i-increase-the-jvm-memory
祝你好运
答案 2 :(得分:-1)
您可以使用以下vm args来增加jvm内存。
-vmargs
-Xmx512m
-XX:MaxPermSize=384m
同样,要指定的值基于RAM内存的大小。对于具有3GB RAM / 32位
的机器,上述值是最佳的这些vm args需要作为命令行参数传递给java应用程序。如果您使用的是eclipse,请在运行配置中指定。