Android 内存不足错误
当尝试使用 SAXParser 解析大型 XML 文件时,我的应用程序崩溃了。
我在 SO 中发现了一个关于它的问题这里未得到解答。
当我减小 XML 的大小时,它按预期工作
这是我的 LogCat 输出。
11-26 12:08:45.099: WARN/dalvikvm(218): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)
11-26 12:08:45.109: ERROR/AndroidRuntime(218): Uncaught handler: thread Thread-9 exiting due to uncaught exception
11-26 12:08:45.220: ERROR/AndroidRuntime(218): java.lang.OutOfMemoryError
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Object.internalClone(Native Method)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Object.clone(Object.java:82)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:141)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.StringBuilder.append(StringBuilder.java:282)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.SaxXMLHandler.characters(SaxXMLHandler.java:24)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:166)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.append(Native Method)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at javax.xml.parsers.SAXParser.parse(SAXParser.java:240)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.SaxParser.parse(SaxParser.java:22)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.iOPEXActivity$4.run(iOPEXActivity.java:112)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Thread.run(Thread.java:1060)
这里是我用来解析的代码
My application is crashing when trying to parse a large XML file using SAXParser.
I found a question in SO regarding it here unanswered.
It is working as intended when i reduce the size of the XML
Here is my LogCat output.
11-26 12:08:45.099: WARN/dalvikvm(218): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)
11-26 12:08:45.109: ERROR/AndroidRuntime(218): Uncaught handler: thread Thread-9 exiting due to uncaught exception
11-26 12:08:45.220: ERROR/AndroidRuntime(218): java.lang.OutOfMemoryError
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Object.internalClone(Native Method)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Object.clone(Object.java:82)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:141)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.StringBuilder.append(StringBuilder.java:282)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.SaxXMLHandler.characters(SaxXMLHandler.java:24)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:166)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.append(Native Method)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at javax.xml.parsers.SAXParser.parse(SAXParser.java:240)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.SaxParser.parse(SaxParser.java:22)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at com.google.android.iopex.iOPEXActivity$4.run(iOPEXActivity.java:112)
11-26 12:08:45.220: ERROR/AndroidRuntime(218): at java.lang.Thread.run(Thread.java:1060)
HERE is the code that i'm using to parse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android 有堆限制,从文件中读取块中的 XML 数据并方便地解析它
Ninja 编辑:
根据 Tseng :通过读取 20-30 个条目来缓存
List
更有意义。There are heap restrictions for android, read the XML data in chunks from your file and parse it conveniently
Ninja edit:
As per Tseng : Caching
List<DbEntry>
by reading 20-30 entries into it makes much more sense.