2

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?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
bala
  • 23
  • 7
  • How much memory space did you allow for java runtime ? Try increasing the memory :) – ykatchou Sep 05 '11 at 12:47
  • 1
    Please specify more details like what approach you are using (Buffered based or stream based or channel based ) – Santosh Sep 05 '11 at 12:50
  • i already tried with increasing the jvm size in the tomcat catalina.bat file now i gave 512m, and i am using javax.xml.transform.stream.StreamResult this class and using HtmlExporterNG2 class again converting the compared document into html file. – bala Sep 05 '11 at 13:02
  • Are you running out of memory during the comparison, or the html conversion? Are you sure you are specifying -Xmx to Tomcat correctly? How many pages are the source docx you are comparing? – JasonPlutext Sep 05 '11 at 13:30
  • error comes when executing the compare method itself, and i am comparing 10 page docx files.this is format in catalina file – bala Sep 05 '11 at 14:05
  • JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xmx512M -Xss1024K -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC" – bala Sep 05 '11 at 14:05
  • What does your code invoking diffx look like? – JasonPlutext Sep 05 '11 at 21:40
  • Re-asking the same question isn't acceptable behavior here. If you want to draw attention to your question, you will be allowed to [place a bounty on it](http://stackoverflow.com/faq#bounty) after two days. You can also [edit your question](http://stackoverflow.com/posts/7308299/edit) to add additional information, which may make your question easier to understand and answer. –  Sep 06 '11 at 13:58

4 Answers4

2

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);
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • i changed my code and now i am using the Docx4jDriver and i am doing the paragraph level comparison but still it show heap memory error after 330 lines before it was 150. – bala Sep 13 '11 at 04:33
0

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

Here more about Heap Size Tuning or here Heap size

Neifen
  • 2,546
  • 3
  • 19
  • 31
  • i already tried with increasing the jvm size in the tomcat catalina.bat file now i gave 512m, and i am using javax.xml.transform.stream.StreamResult this class and using HtmlExporterNG2 class again converting the compared document into html file – bala Sep 05 '11 at 13:03
  • try to increase the stack size with XSS (try as example: -Xmx512M -Xss1024K) [example from here](http://www.docx4java.org/forums/docx-java-f6/problems-with-converting-to-html-t17.html#p36) and personaly I increase in my big project the initial java heap size too (-Xms256m) – Neifen Sep 05 '11 at 13:30
  • i respect your concern about my issue and in my catalina.bat file that size property in this format, JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xmx512M -Xss1024K -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC" – bala Sep 05 '11 at 13:58
  • 1
    You are very likely running out of Perm-Space, try to read your exception better in future ;D This switch: `-XX:MaxPermSize=1024m` is the most important one. If you do something wrong, Catalina should not start but issue an errror. – Angel O'Sphere Sep 05 '11 at 14:30
0

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.

Matthew Blackford
  • 3,041
  • 1
  • 24
  • 28
0

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.

Scorpion
  • 3,938
  • 24
  • 37