使用DOCX4J进行DOCX比较时OutofMemoryError
题
在我的应用程序中,我正在比较两个DOCX文件并创建一个HTML比较文件,当我尝试使用以下150或170行的文件进行尝试时,就没有问题,而我尝试比较200行或更多的大文件或更多的行时间显示
java.lang.OutOfMemoryError: Java heap space error,
有人可以为此提供帮助吗?
解决方案
您之所以用完存储器,是因为您不使用docx4jdriver类,这使差异问题首先通过段落级别的diff来更加处理。
像这样使用:
Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody();
Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody();
// 2. Do the differencing
java.io.StringWriter sw = new java.io.StringWriter();
Docx4jDriver.diff( XmlUtils.marshaltoW3CDomDocument(newerBody).getDocumentElement(),
XmlUtils.marshaltoW3CDomDocument(olderBody).getDocumentElement(),
sw);
// 3. Get the result
String contentStr = sw.toString();
System.out.println("Result: \n\n " + contentStr);
Body newBody = (Body) org.docx4j.XmlUtils
.unmarshalString(contentStr);
其他提示
尝试使用命令行参数增加Java堆大小 -Xmx<maximum heap size>
和 -Xms<minimum heap size>
.
另外,在您的代码中,测试您实际上增加了堆的大小:
long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Heap Size = " + heapSize);
在打电话之前这样做 Differencer.diff
在第117行。
尝试分析您的应用程序,而不是做出假设或聪明的猜测。您可以使用与JDK一起运送的VisualVM或控制台。
另外,您可以使用JMAP进行应用程序的堆转储,然后使用JHAT或ECLIPSE MAT(我更喜欢这个,Google It Out)来查看正在消耗的内存并注意任何异常行为。
不隶属于 StackOverflow