Search code examples
javaxmlunit

Getting Out of memory error while comparing two large xml files


I am trying to compare two large XML files uisng XMLUNIT, but I am getting "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"

public static void main(String[] args) throws Exception {
    FileReader file = new FileReader("C:/abc/a.xml");

    FileReader file1 = new FileReader("C:/abc/b.xml");
    assertXMLEquals(file, file1);
}


public static void assertXMLEquals(FileReader expectedXML, FileReader actualXML) throws Exception {

        DetailedDiff difference = null;
        try {
            // Checks each Node
            difference = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
        } catch (Exception e) {

        }

        if (!difference.similar()) {
            List<Difference> AllDifferences = difference.getAllDifferences();
            System.out.println("Xml comparison failed because of follwoing error/s : \n"+AllDifferences);
        }
    }

Solution - I have added "-Xms2048M -Xmx2048M" as arguments in Eclipse Run configurations.


Solution

  • I havent used xmlunit. But looking at the xmlunit documentation, it looks like you can use streams as the input.

    eg: BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathname))

    Using streams will not load whole file at once to memory.

    But for the to get a OutOfMemoryError exception the files should be very large. You can also increase the maximum heap size for the application at startup.

    eg: Increasing min heap size (xms) and max heap size (xmx) to 2GB.

    java -Xms2048mm -Xmx2048m