【问题标题】:Getting Out of memory error while comparing two large xml files比较两个大型 xml 文件时出现内存不足错误
【发布时间】:2019-08-23 13:23:20
【问题描述】:

我正在尝试比较两个使用 XMLUNIT 的大型 XML 文件,但我收到“线程“主”java.lang.OutOfMemoryError:Java 堆空间中的异常”

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

解决方案 - 我在 Eclipse 运行配置中添加了“-Xms2048M -Xmx2048M”作为参数。

【问题讨论】:

    标签: java xmlunit


    【解决方案1】:

    我没有使用过xmlunit。但是查看 xmlunit 文档,看起来您可以使用流作为输入。

    例如: BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathname))

    使用流不会一次将整个文件加载到内存中。

    但要获得 OutOfMemoryError 异常,文件应该非常大。您还可以在启动时增加应用程序的最大堆大小。

    例如:将最小堆大小 (xms) 和最大堆大小 (xmx) 增加到 2GB。

    java -Xms2048mm -Xmx2048m

    【讨论】:

    • 我在 Eclipse 运行配置中添加了“-Xms2048M -Xmx2048M”作为参数。它对我有用。谢谢!
    【解决方案2】:

    您是否尝试过使用 SAX 解析器? 它将解决您的问题,因为它不需要将整个文件加载到内存中来处理它。

    顺便说一句,你的文件有多大?

    【讨论】:

    • 我的文件大小是 55MB
    • 您能否提供 SAX 解析器的示例或任何链接?
    • 您可以查看 tutorialspoint 或 journaldev 等网站。示例代码在那里很容易获得。例如journaldev.com/1198/java-sax-parser-example如果您发现使用此功能有任何困难,请告诉我。
    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    相关资源
    最近更新 更多