Big XML文件和OutOfMemoryError [英] Big XML file and OutOfMemoryError

查看:96
本文介绍了Big XML文件和OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java中解析高达500 MB的XML文件。我尝试使用SAX,但它给了我这个错误
java.lang.OutOfMemoryError:Java堆空间
at com.sun.org.apache.xerces.internal.util.XMLStringBuffer.append(Unknown Source)
你能帮帮我吗?
非常感谢。
P.S.较小的XML文件可以正常工作

I’m trying to parse a XML file up to 500 mb in java. I tried to use SAX but it gives me this error java.lang.OutOfMemoryError: Java heap space at com.sun.org.apache.xerces.internal.util.XMLStringBuffer.append(Unknown Source) Can you help me? Thanks a lot. P.S. Smaller XML files works just fine

推荐答案

很可能你没有正确使用SAX,或者你的应用程序不适合流处理。

Most likely you're not using SAX correctly, or your application isn't suited for stream processing.

SAX的重点是避免将整个XML结构保留在内存中,但只有在不保留太多上下文的情况下才能以小块的形式处理XML,如果处理结果要么小于处理过的XML(这样它也不会使用太多内存),或者本身可以传递给收件人或连续写入磁盘。

The whole point of SAX is to avoid keeping the entire XML structure in memory, but that's only possible if you can process the XML in small chunks without keeping much context, and if the result of the processing either is much smaller than the processed XML (so that it does not use too much memory either) or can itself be passed on to a recipient or written to disk continuously.

编辑:你也可能只是内存泄漏,即你不再需要你不需要的数据,防止它被垃圾收集。如果您使用任何列表,地图或集合来处理XML,请确保在开始下一个块之前删除处理一个XML块时添加的任何内容。

It's also possible that you simply have a memory leak, i.e. you're holding on to data that you don't need anymore , preventing it from getting garbage collected. If you use any Lists, Maps or Sets for processing the XML, make sure that anything you add to them while processing one chunk of XML is removed before you start the next chunk.

这篇关于Big XML文件和OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆