OutOfMemoryError while doing docx comparison using docx4j

99封情书 提交于 2019-11-28 09:38:37

问题


in my application i am comparing two docx files and creating one html comparison file, when i tried with below 150 or 170 lines of file then there is no issue, while i try to compare the big files like 200 lines or more than that then that time it showing the

java.lang.OutOfMemoryError: Java heap space error,

can any one please help on this?


回答1:


You are running out of memory because you aren't using the Docx4jDriver class, which makes the diff problem more tractable by doing a paragraph level diff first.

Use it like so:

        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);



回答2:


you can make the heap space bigger with -Xmx and -Xmx as VM Arguments

Here more about Heap Size Tuning or here Heap size




回答3:


Try increasing the Java heap size using the command line arguments -Xmx<maximum heap size> and -Xms<minimum heap size>.

Also in your code, test that you actually have increased the heap size with the following:

long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Heap Size = " + heapSize);

Do this before calling Differencer.diff on line 117.




回答4:


Try profiling your application rather than making assumptions or intelligent guess. You can use visualvm or console that ships with the Jdk.

Also, you can take a heap dump of your application using jmap and then use either jhat or eclipse mat (I prefer this, google it out) to see what's consuming the memory and look out for any unusual behavior.



来源:https://stackoverflow.com/questions/7308299/outofmemoryerror-while-doing-docx-comparison-using-docx4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!