1

I have a requirement to convert JSONObject to JsonObject (Gson specific format) in my project. For this purpose I am using following method to convert for the same

public static  JsonObject getJsonFromJSONObject(JSONObject json) {

        JsonObject jsonObject = null;
        try {
            JsonParser jsonParser = new JsonParser();
            jsonObject = (JsonObject) jsonParser.parse(json.toString());

        }catch (Exception e){

            CLog.e(Constants.LOG_TAG, e.getMessage());
        }
        return jsonObject;
    }

Everything works well during the testing phase of my app. But in some rare phenomenon am getting out of memory exception. I am completely stuck up with this scenario since a week, but still, I couldn't able to find any solution.

Error Which I am getting:

Fatal Exception: java.lang.OutOfMemoryError
      at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
      at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:133)
      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.android.package.http.utils.JSONUtils.getJsonFromJSONObject(Unknown Source)
      at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
      at android.app.ActivityThread.access$1700(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
      at android.os.Handler.dispatchMessage(Handler.java:110)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:5299)
      at java.lang.reflect.Method.invokeNative(Method.java)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
      at dalvik.system.NativeStart.main(NativeStart.java)

The above error has been reflected with 26,000 users out of 0.6 million users. I am clueless where I am lagging to fix this. Any kinda solutions and suggestions would be helpful to me. Thanks in advance.

Community
  • 1
  • 1
Chandru
  • 5,954
  • 11
  • 45
  • 85

1 Answers1

1

Try writing JSONObject to file , and read as jsonObject from the file

for Writing JSONObject

ObjectOutputStream out = null;
        FileOutputStream fos = null;

        try {
            File file = new File(fc.getLocalCache().getAbsolutePath() + "/" + folderId + ".srl");
            fos = new FileOutputStream(file);

            out = new ObjectOutputStream(fos);
            out.writeObject(response.toString());
        } catch (OutOfMemoryError | Exception e) {
            e.printStackTrace();
        }

For Reading object

try {
            FileInputStream isf = new FileInputStream(new File(fc.getLocalCache() + "/" + folderId + ".srl"));
            ObjectInputStream in = new ObjectInputStream(isf);
            String json = (String) in.readObject();
            JsonParser parser = new JsonParser();
            JsonObject o = parser.parse(json).getAsJsonObject();
            //Gson gSon=  new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
            jsonObject = new Item();
            jsonObject.setRawObject(new DefaultSerializer(new DefaultLogger()), o); //new Gson().fromJson(o, Item.class);
            in.reset();
            in.close();
            isf.close();
            // Log.i("jsonObject", jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
            //Log.i("getObjectsFromLocal Exception", e.toString());
            return jsonObject;
        }

NOTE To generate out of memory exception you may need low memory mobile and json size at greater then 2 or 3MB.( this may work for you )

IshRoid
  • 3,696
  • 2
  • 26
  • 39