简体   繁体   English

JSONObject.toString() 返回 OutOfMemoryError

[英]JSONObject.toString() returns OutOfMemoryError

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.在这个过程中,服务器向设备发送一个 JSON 对象作为字符串,它可以通过它构建可用的问卷。

GetQuestionnairesResponse.java:获取问卷响应.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.我从服务器得到的结果将其解析为 Json 数组。 The result in some cases can bee 3 megabytes.在某些情况下,结果可能是 3 兆字节。

The solution I found was to add an attribute in manifest.xml:我找到的解决方案是在 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问题是 json 对象没有按预期解析,所以它

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 .如果 JSON 最初是 3 MB 并且您在从中解析出的JSONObject上调用toString() ,则JSONObject仍在占用内存,而且它需要进行 3 MB 分配来保存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.但是关于OutOfMemoryError的事情是,用完最后一位 RAM 的分配不一定是可用 RAM 太少的罪魁祸首。 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.您的应用程序中的其他地方可能存在内存泄漏,您需要使用Eclipse MAT 之类的工具来找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM