1

I am using volley to send some pretty big JsonRequest, and when the Json object calls toString, I receive an java.lang.OutOfMemoryError. It is on Nexus 7 2013 Android 4.4

JsonRequest request = new JsonRequest<ResponseData>(
            method,
            url,
            EntryJsonObject.toString(),
            responseListener,
            errorListener)



java.lang.OutOfMemoryError
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
        at java.lang.StringBuilder.append(StringBuilder.java:124)
        at org.json.JSONStringer.string(JSONStringer.java:344)
        at org.json.JSONStringer.value(JSONStringer.java:252)
        at org.json.JSONArray.writeTo(JSONArray.java:602)
        at org.json.JSONStringer.value(JSONStringer.java:233)
        at org.json.JSONObject.writeTo(JSONObject.java:672)
        at org.json.JSONObject.toString(JSONObject.java:641)
        at com.dis.project1.restclient.Api.putInput(Api.java:205)

Any idea how to build the final String with all Json data so that I will not receive OutOfMemory. String has about 40 MBytes. It is a bit complex structure with 3 JsonArrays with a coupple of objects.

maxxxo
  • 672
  • 3
  • 10
  • 28

2 Answers2

0

Have you tried to compress your string?

Pseudocode:

byte[] compressedBytes = compress(bigJsonString);
String base64String    = Base64.encode(compressedBytes );
[...]
byte[] origCompressedBytes  = Base64.decode(base64String);

How can I easily compress and decompress Strings to/from byte arrays?


You could also try Json Streaming

Most applications should use only the object model API. JSON streaming is useful in just a few situations:

  • When it is impossible or undesirable to load the entire object model into memory. This is most relevant on mobile platforms where memory is limited.
  • When it is necessary to read or write a document before it is completely available.
Community
  • 1
  • 1
alex
  • 8,904
  • 6
  • 49
  • 75
-1

Just add android:largeHeap="true" line in your application AndroidMenifest.xml . It might help you.

Like :

<application
        android:name=""
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        **android:largeHeap="true"**
         ...
    </application>
Darsh Patel
  • 1,015
  • 9
  • 15