Android OutOfMemoryError - 加载JSON文件 [英] Android OutOfMemoryError - Loading JSON File

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

问题描述

我正在开发的应用程式需要读取JSON档案,档案大小可能介于1.5到3 MB之间。它似乎没有问题打开文件并将数据转换为字符串,但是当它试图将字符串转换为JSONArray时,抛出OutOfMemoryErrors。异常类似这样:

The app I am working on needs to read a JSON file that may be anywhere from 1.5 to 3 MB in size. It seems to have no problem opening the file and converting the data to a string, but when it attempts to convert the string to a JSONArray, OutOfMemoryErrors are thrown. The exceptions look something like this:

E/dalvikvm-heap( 5307): Out of memory on a 280-byte allocation.
W/dalvikvm( 5307): Exception thrown (Ljava/lang/OutOfMemoryError;) while throwing internal exception (Ljava/lang/OutOfMemoryError;)

一个奇怪的事情是,崩溃只发生在应用程序运行的第二或第三次,让我相信应用程序消耗的内存不是垃圾收集每个应用关闭的时间。

One strange thing about this is that the crash only occurs every 2nd or 3rd time the app is run, leaving me to believe that the memory consumed by the app is not being garbage collected each time the app closes.

对我如何解决这个问题的任何洞察将非常感谢。我开放的想法加载文件在块,但我不知道什么最好的方法是这样的任务。

Any insight into how I might get around this issue would be greatly appreciated. I am open to the idea of loading the file in chunks, but I'm not quite sure what the best approach is for such a task.

谢谢

推荐答案


奇怪的是,崩溃只发生在应用程序运行的第二或第三次,让我相信应用程序消耗的内存不是垃圾收集每次应用程序关闭。

One strange thing about this is that the crash only occurs every 2nd or 3rd time the app is run, leaving me to believe that the memory consumed by the app is not being garbage collected each time the app closes.

这是可能的,如果是这样,那么可能是由于内存泄漏,可以追溯到你的应用程序正在做。我认为你应该集中你的初步努力调查这方面,而不是加载文件在块。 (我不熟悉Android工具链,但我确定它包括内存使用情况分析器或内存转储分析器。)

That is certainly possible, and if it is the case then it probably due to a memory leak that can be traced back to something that your application is doing. I think you should focus your initial efforts into investigating this aspect ... rather than loading the file in chunks. (I am not familiar with the Android tool-chain, but I am sure it includes memory usage profilers or memory dump analysers.)

EDIT

为了回应您的后续评论,它在3中工作两次的事实表明您的应用应该按原样工作。当然,如果输入文件变大,你没有太多的余地。

In response to your followup comment, the fact that it works 2 times in 3 suggests that your app ought to work roughly as-is. Admittedly, you don't have much leeway if the input file gets bigger.

有几个想法:


  • 不要将文件读入String并在String上运行JSON解析器,而是使用可以直接从流读取的解析器。

  • Rather than reading the file into a String and running the JSON parser on the String, use a parser that can read directly from a stream. Your current solution needs space for two complete copies of the data in memory while you are doing the parsing.

如果文件变得更大,您可能需要想到一个不能创建完整的内存中数据表示的设计。

If the file gets much bigger, you may need to think of a design that doesn't create a complete in-memory representation of the data.

我不确定在chunks中读取JSON文件是个好主意。这可能会出现解析JSON的问题...根据你的意思,通过阅读块。

I'm not sure that it is a good idea to read a JSON file in "chunks". This could present problems for parsing the JSON ... depending on exactly what you mean by reading in chunks.

编辑2

也许你需要的是一个SAX likeJSON解析器例如 http://code.google.com/p/async-json-library/

Maybe what you need is a "SAX like" JSON parser; e.g. http://code.google.com/p/async-json-library/

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

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