解析大型Json字符串会导致OOM错误

时间:2015-07-20 09:15:27

标签: android json string out-of-memory asynctaskloader

我有一个新闻应用程序,通过JSON获取其内容,有时它解析非常大的字符串。我有一个对象,其中包含所有文章的数据,然后将其保存到数据库中。

然而,有时,我得到了对象类的内存不足异常。以下是logcat:

java.lang.RuntimeException: An error occured while executing doInBackground()
       at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 230 byte allocation with 96 free bytes and 96B until OOM
       at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
       at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:163)
       at java.lang.StringBuilder.append(StringBuilder.java:288)
       at com.android.internal.os.LoggingPrintStream.append(LoggingPrintStream.java:333)
       at com.android.internal.os.LoggingPrintStream.append(LoggingPrintStream.java:36)
       at java.lang.Throwable.printStackTrace(Throwable.java:315)
       at java.lang.Throwable.printStackTrace(Throwable.java:282)
       at java.lang.Throwable.printStackTrace(Throwable.java:236)
       at com.radioafrica.mpasho.object.Article.(Article.java:31)
       at com.radioafrica.mpasho.loader.FeedsLoader.loadInBackground(FeedsLoader.java:50)
    at com.radioafrica.mpasho.loader.FeedsLoader.loadInBackground(FeedsLoader.java:24)
       at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)
       at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
       at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
       at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:818)

当我尝试在将数据传递给适配器之前通过asynctaskloader加载数据时发生错误。

0 个答案:

没有答案