开发者社区> 问答> 正文

紧急求救:android json 内存溢出

screenshot
代码如下:
public JSONObject SetDataToJson()
{
JSONObject LCYDataJSON = new JSONObject();

    try {   
        LCYDataJSON.put("MODE", lcydata.mode);
        LCYDataJSON.put("date", lcydata.date); 
        LCYDataJSON.put("GPSSta", lcydata.gpsstat);
        LCYDataJSON.put("lat", Float.toString(lcydata.lati));
        LCYDataJSON.put("longg",  Float.toString(lcydata.longg));
        LCYDataJSON.put("ADDRESS", lcydata.addr);
        LCYDataJSON.put("WIFINUM", Integer.toBinaryString(lcydata.wifinum));
    } catch (JSONException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
        LCYDataJSON = null;
        return null;
    }
    JSONArray AgentArray = new JSONArray();
    for(int i = 0;i <lcydata.agent.length;i++)
    {
        if(lcydata.agent[i]==null)
        {
            continue;
        }
        //如果是错误的,就是根本没有采集到则不进行json打包
        if(lcydata.agent[i].LAC == -1&&lcydata.agent[i].CI==-1)
        {
            continue;
        }
        JSONObject AgentIF = new JSONObject();
        try {

            AgentIF.put("Agent",lcydata.agent[i].Agent);
            AgentIF.put("LAC",lcydata.agent[i].LAC);
            AgentIF.put("CI",lcydata.agent[i].CI);
            AgentIF.put("FREQ",lcydata.agent[i].FREQ);
            AgentIF.put("RSCP",lcydata.agent[i].RSCP);
            AgentIF.put("LACCHG",lcydata.agent[i].LACCHG);
            AgentIF.put("tasktype",lcydata.agent[i].tasktype);//中标类型
            AgentIF.put("type",lcydata.agent[i].type);//邻区类型
            AgentIF.put("NibNum",lcydata.agent[i].NibNum);//邻区个数
            AgentIF.put("bSameAsBefore", lcydata.agent[i].bSameAsBefore);//是否时和上包数据重复

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            Log.d(TAG,"AgentIF Handle Catch error");
            e1.printStackTrace();
            AgentArray = null;
            AgentIF = null;
            return null;
        }

        JSONArray AgentNArray = new JSONArray();
        //邻区处理
        for(int j=0;j<lcydata.agent[i].NibNum;j++)
        {  
            JSONObject AgentNIF = new JSONObject();
            try {

             AgentNIF.put("LAC",lcydata.agent[i].agentn[j].LAC);
             AgentNIF.put("CI",lcydata.agent[i].agentn[j].CI);
             AgentNIF.put("RSCP",lcydata.agent[i].agentn[j].RSCP); 
             AgentNIF.put("tasktype", lcydata.agent[i].agentn[j].TaskType);
             AgentNArray.put(AgentNIF);
             AgentNIF = null;
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.d(TAG,"AgentNIF handle Catch error");
                e.printStackTrace();
                AgentNIF = null;
                LCYDataJSON = null;
                return null;
            }

        }

        try {
            AgentIF.put("AgentN",AgentNArray);   
            AgentArray.put(AgentIF);
            LCYDataJSON.put("Agent",AgentArray);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.d(TAG,"Add LCDataJson catch error");
            e.printStackTrace();
            for(int ii = 0; ii< lcydata.agent[ii].NibNum;ii++)
            {
                for(int m = 0 ;m<lcydata.agent[ii].NibNum;m++)
                {
                    lcydata.agent[ii].agentn[m] = null;
                }

            }
            LCYDataJSON = null;
            return null;
        }

    }

    //wifi信息集合
    if(lcydata.wifinum > 0)
    {
     JSONArray WIFIArray = new JSONArray();
     try
     {

        for(int i=0;i<lcydata.wifinum;i++)
        {
         JSONObject WIFIIF = new JSONObject();
        //WIFIIF = lcydata.wifiinfo[i]
         WIFIIF.put("SSID",lcydata.wifiinfo[i].SSID);
         WIFIIF.put("MAC",lcydata.wifiinfo[i].MAC);
         WIFIIF.put("RSCP",lcydata.wifiinfo[i].RSCP);
         WIFIIF.put("PerCent",lcydata.wifiinfo[i].PerCent);
         WIFIIF.put("IDEN",lcydata.wifiinfo[i].IDEN);
         WIFIIF.put("Encrypt",lcydata.wifiinfo[i].Encrypt);
         WIFIArray.put(WIFIIF);
        //释放内存
         lcydata.wifiinfo[i] = null;
         WIFIIF = null;
       } 
        LCYDataJSON.put("WifiInfo",WIFIArray);
        WIFIArray = null;
      } catch (JSONException e)
      {
            // TODO Auto-generated catch block
          Log.d(TAG,"WIFIArray handle catch error ");
          e.printStackTrace();
            for(int ii = 0; ii< lcydata.agent[ii].NibNum;ii++)
            {
                for(int m = 0 ;m<lcydata.agent[ii].NibNum;m++)
                {
                    lcydata.agent[ii].agentn[m] = null;
                }

            }
          WIFIArray = null;
          LCYDataJSON = null;
          return null;

      //  System.out.println(LCYDataJSON.toJSONString());
      }
     }
       ThreadInsertDb(LCYDataJSON);
       Intent i = new Intent(Const.DATABC);//广播消息,实现类之间的交互 
       Bundle mBundle = new  Bundle();
       //进行数据反序列化,还原数据
       mBundle.putString("JSONOBJSTR", LCYDataJSON.toString());  
       i.putExtras(mBundle);
       //释放内存
       LCYDataJSON = null;
      //给系统配置广播数据
     //给实时数据fragement广播数据
    if(null!= FragmentRealData.s_context)
    {
    //      Log.d(TAG, "Broad to FragmentRealData:"+8); 
            FragmentRealData.s_context.sendBroadcast(i);
            i = null;
            mBundle = null;
    }
    //因为时一个activiy--mainactivity 所以这里只广告一次就都能收到
/*  if(null!=FragmentRealWifiMap.s_context)
    {
        FragmentRealWifiMap.s_context.sendBroadcast(i);
    }
    if(null!= FragmentRealDataMap.s_context)
    {
        //FragmentRealDataMap.s_context.sendBroadcast(i);
    }*/
    for(int ii = 0; ii< lcydata.agent[ii].NibNum;ii++)
    {
        for(int m = 0 ;m<lcydata.agent[ii].NibNum;m++)
        {
            lcydata.agent[ii].agentn[m] = null;
        }

    }
    if(lcydata.wifinum > 0)
    {
        for(int l = 0; l < lcydata.wifinum;l++)
        {
            lcydata.wifiinfo[l] = null;

        }
    }
    System.gc();
    return LCYDataJSON;
}
    请大神门帮看看,哪里出问题了!!![图片说明](http://img.ask.csdn.net/upload/201601/20/1453252530_369935.png)

展开
收起
杨冬芳 2016-07-06 19:21:08 3395 0
1 条回答
写回答
取消 提交回答
  • IT从业

    简单看了下代码没看出什么,但是看你的图片感觉很奇怪,为什么每一个nameValuePairs都是同一个对象?(@ 0x4325c010 表示对象的内存地址,所有的地址都一样,说明是同一个对象)。另外你调试时有跟踪、展开你的JSON对象检查内部的信息是否正确么?还有你说JSON对象内存溢出了,Exception是什么?堆栈信息是什么?

    2019-07-17 19:52:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载