2

I have an Android app. First, the app do sync process. In this process, the server sends to the device an JSON object as String by which it can build the 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 from server

    // Creating JSON Parser instance
    try
    {
        QuestionnaireDataSource questionnaireDS = new QuestionnaireDataSource(caller);
        questionnaireDS.open();
        JSONArray jArr = new JSONArray(result);
        JSONObject j = null;
        totalNumOfQuestionnaires = jArr.length();
        for (int i = 0; i < 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 the 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 the 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 i get from the server i parse it to Json array. The result in some cases can bee 3 megabytes.

The solution I found was to add an attribute in manifest.xml:

   android:largeHeap="true"

It solved the problem. I don't know why but the problem returned again in the last day. I will be happy to get suggestions how to solve the problem. The problem is that the json object not parsed as expected so it

3
  • instead of convert Json to object by self use third party library like Gson and Jakson. don't use largHeap for this issue. as you want convert by self you copy all json in to memory and as your json is very large ( in my guess ) you get OOM error Commented Oct 3, 2015 at 7:10
  • Can you show in a small example what do you mean please? Commented Oct 3, 2015 at 7:57
  • see github.com/google/gson, or github.com/kiha/jackson Commented Oct 3, 2015 at 8:52

1 Answer 1

2

If the JSON was originally 3 MB and you call toString() on the JSONObject parsed from it, the JSONObject is still taking up memory, plus it's going to need to do a 3 MB allocation to hold the String. You may not have that much memory available.

But the thing about OutOfMemoryError is that the allocation that uses up the last bit of RAM isn't necessarily to blame for there being so little RAM available. Big allocations are just more likely to be the thing that pushes it over the edge. It's likely that you have a memory leak somewhere else in your app, and you'll need to use a tool like Eclipse MAT to find it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.