JSONObject.toString()返回OutOfMemoryError [英] JSONObject.toString() returns OutOfMemoryError

查看:156
本文介绍了JSONObject.toString()返回OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用. 首先,应用执行同步过程.在此过程中,服务器将String形式的JSON对象发送到设备,通过它可以构建可用的调查表.

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:

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);


    ...
}

我从服务器获得的结果解析为Json数组. 在某些情况下,结果可能是3兆字节.

The result i get from the server i parse it to Json array. The result in some cases can bee 3 megabytes.

我找到的解决方案是在manifest.xml中添加一个属性:

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

   android:largeHeap="true"

它解决了问题.我不知道为什么,但是问题在最后一天又回来了. 我将很乐意为您提供解决问题的建议. 问题在于json对象未按预期进行解析,因此

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最初为3 MB,并且您在解析后的JSONObject上调用toString(),则JSONObject仍会占用内存,再加上它需要分配3 MB的空间来容纳String.您可能没有足够的可用内存.

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.

但是关于OutOfMemoryError的问题是,占用RAM的最后一位的分配不一定要归咎于可用的RAM太少.大的分配更有可能将其推向边缘.您的应用程序其他地方可能存在内存泄漏,因此您需要使用类似 Eclipse MAT的工具找到它.

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.

这篇关于JSONObject.toString()返回OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆