http-request
http-request copied to clipboard
java.lang.OutOfMemoryError in java.lang.String.<init> when downloading json files
Some of my users are reporting out of memory errors. Is there a way to optimize the method ByteArrayOutputStream?
stack trace:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at java.lang.String.
Perhaps try using stream()
/reader()
instead, what JSON library are you using?
It probably supports streaming using a InputStreamReader
and/or InputStream
instead of parsing one big String
.
i am using Android's copy of JSONTokener Seems to be no way to insert a stream in that jsontokener lib. Only strings.
I think you might workaround this by using JsonReader from the Gson library.
The body(final String charset)
method returns a String object so this can throw OOM exception I think.
Have the same issue btw.