0

I have a news app that gets its content via JSON and at times it parses very large strings. I have an object that holds all the article's data which is then saved to a database.

However, at times, I get an out of memory exception with the object class. The following is the 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)

The error occurs when i try to load the data via an asynctaskloader before passing the data to the adapter.

Alex Kombo
  • 3,256
  • 8
  • 34
  • 67
  • Just an idea:: If your string is too large means you can split it and use. – Amsheer Jul 20 '15 at 09:19
  • @Amsheer some articles are longer than others hence the large strings. Since the error occurs when i try to retrieve the string from the database, wont i still get the same error. Or are you suggesting i split it before i save it to the database? – Alex Kombo Jul 20 '15 at 09:25
  • Actually what i mean was when you read from json split it or before receive server side split it. Now the string are small do whatever you want? – Amsheer Jul 20 '15 at 09:34
  • I would probably just use something like Gson, that supports streams. See: https://sites.google.com/site/gson/streaming – Darwind Jul 20 '15 at 09:56
  • let me try out streaming since i am using gson anyway for parsing. – Alex Kombo Jul 20 '15 at 10:50

0 Answers0