安卓的OutOfMemoryError - 装载JSON文件 [英] Android OutOfMemoryError - Loading JSON File

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

问题描述

该应用程序我工作的阅读需求,可能是从1.5到3 MB大小的任何地方的JSON文件。它似乎已经没有问题打开文件和数据转换为字符串,但是当它试图将字符串到JSONArray转换,OutOfMemoryError异常抛出。唯一的例外是这个样子:

  E / dalvikvm堆(5307):内存在280字节的分配。
W / dalvikvm(5307):异常抛出(Ljava /朗/ OutOfMemoryError错误)而引发内部异常(Ljava /朗/ OutOfMemoryError异常;)
 

一个奇怪的事情有关,这是该崩溃只发生每2或第3次应用程序运行时,让我相信,通过应用程序所消耗的内存不被垃圾收集每一个应用程序关闭的时间。

任何深入了解我可能会解决此问题将大大AP preciated。我打开装在块文件的想法,但我不是很确定最好的办法是什么这样的任务。

感谢您

解决方案
  

一个奇怪的事情有关,这是该崩溃只发生每2或第3次应用程序运行时,让我相信,通过应用程序所消耗的内存不被垃圾收集每一个应用程序关闭的时间。

这当然是可能的,如果是的话,那么它可能是由于可追溯至东西,你的应用程序在做内存泄漏。我想你应该专注于你最初的努力为调查这方面......而不加载块文件。 (我不熟悉Android工具链,但我相信它包括内存使用分析器或内存转储分析仪。)

修改

在回答您的后续评论,它的工作原理2次3的事实表明,你的应用程序应该工作大致原样。诚然,你没有太多的回旋余地,如果输入文件变大。

一对夫妇的想法,但:

  • 而不是把文件读入一个字符串,并运行字符串的JSON解析器,使用解析器可以直接从流中读取。您当前的解决方案需要的空间数据在内存中两个完整的副本,而你正在做的解析。

  • 如果该文件变得更大,你可能需要考虑不建立一个完整的内存重新$ P $的数据psentation设计的。

我不知道这是读一个JSON文件中的块是个好主意。这可能present问题解析JSON的...取决于正是通过读块的意思。

编辑2

也许你需要的是一个SAX像JSON解析器;例如 HTTP://$c$c.google.com/p/async- JSON库/

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.

Thank you

解决方案

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.

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

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.

A couple of ideas though:

  • 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.

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.

EDIT 2

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

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

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