XML解析java.lang.OutOfMemoryError:[内存耗尽]

时间:2013-01-11 10:34:58

标签: android android-layout

您好我正在通过以下代码解析xml

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputStream is = getResources().openRawResource(R.raw.bd);
    try {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(is, null);
        doc.getDocumentElement().normalize();
        NodeList my_words = doc.getElementsByTagName("word");
        for (int i = 0; i < 100; i++) {
            System.out.println(((Element) my_words.item(i)).getAttribute("eng"));
            System.out.println(((Element) my_words.item(i)).getAttribute("guj"));
        }
        is.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

但我得到的错误如下......

          01-11 16:00:48.709: E/dalvikvm-heap(18535): Out of memory on a 176-byte allocation.
          01-11 16:00:48.709: I/dalvikvm(18535): "main" prio=5 tid=1 RUNNABLE
          01-11 16:00:48.709: I/dalvikvm(18535):   | group="main" sCount=0 dsCount=0 obj=0x40022190 self=0xd018
          01-11 16:00:48.709: I/dalvikvm(18535):   | sysTid=18535 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1345002400
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.nativeFillInStackTrace(Native Method)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.fillInStackTrace(Throwable.java:135)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.<init>(Throwable.java:74)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Error.<init>(Error.java:38)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.VirtualMachineError.<init>(VirtualMachineError.java:36)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.OutOfMemoryError.<init>(OutOfMemoryError.java:34)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.util.ArrayList.add(ArrayList.java:-1)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.dom.ElementImpl.setAttributeNode(ElementImpl.java:329)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:355)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:360)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:133)
          01-11 16:00:48.709: I/dalvikvm(18535):   at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:132)
          01-11 16:00:48.709: I/dalvikvm(18535):   at com.example.simplexmlpullapp.MainActivity.onCreate(MainActivity.java:32)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.access$1500(ActivityThread.java:123)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.os.Handler.dispatchMessage(Handler.java:99)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.os.Looper.loop(Looper.java:130)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.main(ActivityThread.java:3835)
         01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.reflect.Method.invokeNative(Native Method)
         01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.reflect.Method.invoke(Method.java:507)
         01-11 16:00:48.709: I/dalvikvm(18535):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
         01-11 16:00:48.709: I/dalvikvm(18535):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
         01-11 16:00:48.709: I/dalvikvm(18535):   at dalvik.system.NativeStart.main(Native Method)
         01-11 16:00:48.729: W/dalvikvm(18535): Exception thrown (Ljava/lang/OutOfMemoryError;) while throwing internal exception (Ljava/lang/OutOfMemoryError;)
        01-11 16:00:48.729: W/dalvikvm(18535): Could not resolve class ref'ed in exception catch list (class index 349, exception Ljava/lang/OutOfMemoryError;)
        01-11 16:00:49.209: I/dalvikvm-heap(18535): Clamp target GC heap from 25.906MB to 24.000MB
        01-11 16:00:49.209: D/dalvikvm(18535): GC_CONCURRENT freed <1K, 11% free 22006K/24519K, external 3K/514K, paused 5ms+6ms
        01-11 16:00:49.509: D/dalvikvm(18535): GC_FOR_MALLOC freed 19308K, 89% free 2698K/24519K, external 3K/514K, paused 294ms
        01-11 16:00:49.509: D/AndroidRuntime(18535): Shutting down VM
        01-11 16:00:49.509: W/dalvikvm(18535): threadid=1: thread exiting with uncaught exception (group=0x40018560)
        01-11 16:00:49.509: E/AndroidRuntime(18535): FATAL EXCEPTION: main
        01-11 16:00:49.509: E/AndroidRuntime(18535): java.lang.OutOfMemoryError: [memory exhausted]
        01-11 16:00:49.509: E/AndroidRuntime(18535):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

Java中的SAX XML解析器

SAX代表XML解析的简单API。这是一个基于事件的XML Parsing,它逐步解析XML文件,非常适合大型XML文件。 SAX XML Parser在遇到打开标记,元素或属性时触发事件,并且解析相应地工作。建议使用SAX XML解析器来解析Java中的大型xml文件,因为它不需要在Java中加载整个XML文件,并且它可以读取一小部分的大型XML文件。 Java提供了对SAX解析器的支持,您可以使用SAX Parser解析Java中的任何xml文件,我已经介绍了使用SAX Parser读取xml文件的示例。在java中使用SAX Parser的一个缺点是,使用SAX Parser在Java中读取XML文件需要比DOM Parser更多的代码。

DOM和SAX XML Parser之间的区别 以下是Java解析器和Java中的SAX解析器之间的几个高级别差异:

1)DOM解析器在内存中加载整个xml文档,而SAX只在内存中加载一小部分XML文件。

2)DOM解析器比SAX更快,因为它可以访问内存中的整个XML文档。

3)Java中的SAX解析器比DOM Parser更适合大型XML文件,因为它不需要太多内存。

4)DOM解析器适用于文档对象模型,而SAX是基于事件的xml解析器。

这完全取决于Java中的SAX和DOM解析器之间的区别,现在由您决定选择哪种XML解析器。如果XML文件足够小,我建议在SAX解析器上使用DOM解析器,如果您不知道要处理的xml文件的大小或者它们很大,请使用SAX解析器。

答案 1 :(得分:1)

根据此answer,我在项目中使用了XmlPullParser。它比SAX和DOM更容易使用。您可以找到有关Android Developers SiteTutorial at Vogella.com

的更多信息