yuhan506 2016-01-20 01:15 采纳率: 0%
浏览 2089

紧急求救:android json 内存溢出

图片说明
代码如下:
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;
    }
        请大神门帮看看,哪里出问题了!!![图片说明](https://img-ask.csdn.net/upload/201601/20/1453252530_369935.png)
  • 写回答

3条回答

  • tony4geek 2016-01-20 01:29
    关注

    是数据本身多,还是代码引起的呢?

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面