我正在使用 xstream 将对象序列化为 xml 作为文件。我要序列化的对象非常大,由一个非常复杂的树结构组成。为此,我使用以下代码:
XStream xStream = new XStream();
xStream.omitField(Stat.class, "sources");
String XMLFilename = "XML-" + dateString + ".xml";
FileOutputStream outputStream = null;
Writer writer = null;
try {
outputStream = new FileOutputStream(currentWorkPath+XMLFilename);
writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
xStream.toXML(ss, writer);
} catch (Exception exp) {
log.error(null, exp);
} finally {
writer = null;
outputStream = null;
}
代码工作正常,但是在序列化非常大的对象时出现内存错误 java 堆空间不足。有什么方法可以防止这种情况吗?随着对象的增长,是否可以动态构建 xml?
或者可能是其他一些 xml 序列化库?
例外:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.HashMap.addEntry(Unknown Source)
at java.util.HashMap.put(Unknown Source)
at com.thoughtworks.xstream.core.util.ObjectIdDictionary.associateId(ObjectIdDictionary.java:91)
谢谢,詹姆斯福特