java - JSONObject.toString() returns OutOfMemoryError -


i have android app. first, app sync process. in process, server sends device json object string can build available questionnaires.

getquestionnairesresponse.java:

public class getquestionnairesresponse extends responsehandler {  public getquestionnairesresponse(string result, asyncrequest request) {     super(result, request); }  @override public void handleresponse() {     datasyncactivity caller = (datasyncactivity) request.getcaller();     backgroundmanager bckmng = backgroundmanager.getinstance(caller);     preferencesmanager preference = preferencesmanager.getinstance(null);      boolean status = true;     int numofwrongjsonver = 0;     int totalnumofquestionnaires = 0;     // handle data server      // creating json parser instance     try     {         questionnairedatasource questionnaireds = new questionnairedatasource(caller);         questionnaireds.open();         jsonarray jarr = new jsonarray(result);         jsonobject j = null;         totalnumofquestionnaires = jarr.length();         (int = 0; < jarr.length(); i++)         {             j = jarr.getjsonobject(i);             long questid = j.getlong("questionnaireid");             long surveyid = j.getlong("surveyid");             string questname = j.getstring("name");             string desc = j.getstring("description");             int version = j.getint("questionnaireversion");             int jsonversion = j.getint("jsonversion");              if (jsonversion == preferencesmanager.jsonversion)             {                 // save pages part                 string filename = questid + "_" + version + "_" + jsonversion + ".json";                 helpfunctions.writejson(filename, j.tostring());                  questionnaire quest = questionnaireds.createquestionnaire(questid, questname, desc, surveyid, version, jsonversion, filename);                  if (quest == null)                     throw new runtimeexception("cant save questionnaire: " + questname);             }             else             {                 numofwrongjsonver ++;             }         }         questionnaireds.close();     }     catch (exception e)     {         status = false;         if (e.getmessage() != null)             log.e(tag, e.getmessage());     }      caller.setinsync(false);       ... } 

the result server parse json array. result in cases can bee 3 megabytes.

the solution found add attribute in manifest.xml:

   android:largeheap="true" 

it solved problem. don't know why problem returned again in last day. happy suggestions how solve problem. problem json object not parsed expected

if json 3 mb , call tostring() on jsonobject parsed it, jsonobject still taking memory, plus it's going need 3 mb allocation hold string. may not have memory available.

but thing outofmemoryerror allocation uses last bit of ram isn't blame there being little ram available. big allocations more thing pushes on edge. it's have memory leak somewhere else in app, , you'll need use tool eclipse mat find it.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -