0

I am trying to write large JSONArray to string and then saved in sdcard. In this process getting OutOfMemory exception while converting JSONArray to string. The code will be given below. Please anyone say how to overcome this...

Code:

JSONArray loanSurvey = callbackObject.getJSONArray("bankloansurvey");
            try {
                String filename = "surveyfile.txt";
                File myFile = new File(surveyFolder, filename);
                if (!myFile.exists())
                    myFile.createNewFile();
                FileOutputStream fos;
                StringBuilder surveyData = new StringBuilder(result.length());
                surveyData.append(loanSurvey.toString());            // here getting outofmemory error
                byte[] dataArray = surveyData.toString().getBytes();

Error Report:

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.JSONObject.writeTo(JSONObject.java:672)
                                                                        at org.json.JSONStringer.value(JSONStringer.java:237)
                                                                        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.JSONStringer.value(JSONStringer.java:237)
                                                                        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.JSONStringer.value(JSONStringer.java:237)
                                                                        at org.json.JSONArray.writeTo(JSONArray.java:602)
                                                                        at org.json.JSONArray.toString(JSONArray.java:574)

Thanks in advance...

Narendra Kumar
  • 551
  • 5
  • 16

2 Answers2

0

There may be two resons:

  1. when memory leaks in your application.
  2. Devices do not have enough memory when running your application.

Here can't increase the heap size dynamically but you can request to use more by using.

<application
..................
..................
android:largeHeap="true"/>

in the manifest.xml.

Kush
  • 1,080
  • 11
  • 15
-1

If out of memory than perform the operation in thread and increase heap size.

To edit heapsize

Go to android manifest and add android:largeHeap="true" within the application tag

silverFoxA
  • 4,549
  • 7
  • 33
  • 73