带有XML的Android OutOfMemoryError [英] Android OutOfMemoryError with XML

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

问题描述

我正在构建一个从服务器获取XML的应用程序,然后解析它(它是在地方格式化的DIDL)。目标是尽可能多地加载到内存中以使用户更容易浏览(以便快速滚动结果),但我一直得到 OutOfMemoryError s。我对Android很新,看起来虚拟机对于事情真的很挑剔。我加载的不超过一兆字节的XML,并且几乎立即丢弃它。我应该怎么做才能防止这些错误发生?

I'm building an app that fetches XML from a server, then parses it (it's DIDL formatted in places). The goal is to load as much into memory as possible to make it easier for users to browse (so as to enable fast scrolling through results), but I keep getting OutOfMemoryErrors. I'm kind of new to Android, and it seems like the VMs are really finicky about things. I'm loading no more than a megabyte of XML, and pretty much discarding it right away. What should I do to prevent these errors from happening?

我应该通过网络将文件块加载块,写入磁盘,然后按块加载块进出内存,将所有内容解析为POJO?如果可以避免,我不想实现某种形式的分页,根据Twitter应用程序(当你触及底部时它用于加载更多条目,它现在一次性加载很多同样崩溃与一个OOM错误。)如果有帮助,我正在运行Nexus One,CM7 / Android 2.3.3。

Should I load chunk by chunk of the file over the network, write it to disk, then load chunk by chunk back into and out of memory, parsing everything into POJOs? If it can be avoided, I'd like to not have to implement some form of pagination, as per the Twitter app (it used to load more entries when you hit the bottom, it now loads a lot all at once and likewise crashes with an OOM error.) I'm running a Nexus One if that helps, CM7/Android 2.3.3.

推荐答案

你可以读懂这个很好的文章关于Android上的XML解析。使用pull解析器对你来说是个不错的选择,因为它不需要将完整的文档读入内存,这就是你的问题。我建议您将已解析的结果存储到数据库中,因为一旦它们存在,您就可以快速列出它们并以任何方式对它们进行分页,并且DB的性能非常好。这样,您只需要将服务器到数据库的完整数据加载一次(如果服务器上的数据没有变化),或者加载一次,然后在更改时不时获取更新(如果是twitter)。

You can read this nice article about XML parsing on Android. Using pull parser would be good choice for you, since it does not need to read complete document into the memory, which is the problem in your case. I would suggest that you store parsed results into the database, since once they are there, you can quickly do list them and page them any way you want, and performance of the DB is great. This way, you need to do loading of complete data from server to DB just once (if data is not changing on the server) or to load it once and then to get updates from time to time if it is changing (like in case of twitter).

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

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