OutOfMemoryException异常,而试图大JSON数据发送到服务器的Andr​​oid? [英] OutOfMemoryException while trying to send large json data to server in android?

查看:202
本文介绍了OutOfMemoryException异常,而试图大JSON数据发送到服务器的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一时间到PHP服务器发送多个JSON字符串[各1 MB。当我试图发送10条记录其工作罚款。但如果超过20条记录它说 OutOfMemoryException异常。我看到,Android的内存大小限制T0 15MB - 16MB。但没有得到任何线索如何解决这个问题,
我使用的是低于code到一次发送多个记录。

  / **上传数据* /
私人无效submitUploadData(字符串URL,地图<字符串,字符串>参数)
        抛出IOException
    URL SITEURL;
    尝试{
        SITEURL =新的URL(网址);
        HttpURLConnection的康恩=(HttpURLConnection类)SITEURL
                .openConnection();
        conn.setRequestMethod(POST);
        conn.setDoOutput(真);
        conn.setDoInput(真);        DataOutputStream类出=新DataOutputStream类(conn.getOutputStream());
        串内容1 =;        设置getkey = param.keySet();
        迭代器keyIter = getkey.iterator();
        字符串内容=;
        的for(int i = 0; keyIter.hasNext();我++){
            对象键= keyIter.next();
            如果(ⅰ!= 0){
                内容+ =&放大器;; //这里崩溃
            }
            内容+ =键+=+ param.get(键); //这里崩溃
            的System.out.println(内容+内容);
        }        的System.out.println(内容);
        out.writeBytes(content.trim());
        了out.flush();
        out.close();        在的BufferedReader =新的BufferedReader(新的InputStreamReader(
                conn.getInputStream()));
        串jsonresponse =;
        而((jsonresponse = in.readLine())!= NULL){
            的System.out.println(jsonresponse);
            如果(!jsonresponse.equals(授权成功)){
            updateUploadRecords(jsonresponse);}        }
        附寄();    }赶上(MalformedURLException的E){
        e.printStackTrace();
    }
}


解决方案

乍一看没有什么不对您的code,但您使用的是,作为一个内存浪费而现在已知的每一个可能的类型的方法你想你运行内存大作业,这是一定会发生的。

提示:


  • 请不要contatenate与 STR1 + = STR2 +等VAL串您刚刚创建在内存中3个新的字符串。使用StringBuilder来代替。

  • 调用System.gc()的(如一些会建议你)会不会帮助你,因为你是在一个循环做此操作和方法只提示系统,这可能是一个好时机,GC,但直到该系统实际上是避开这样做,你就已经崩溃了。

  • 如果每个JSON字符串是1MB左右,并要传递其中的20个,您已经在与旧设备上的内存不足崩溃就在那里与20MB。你必须重新设计了不少东西,但我的建议是只删除地图,完全删除它。

相反,你应该(在所创建地图的方法)使用某种类型的的OutputStream 来编写所有的数据,已格式化的临时文件。然后,上传您将使用某种类型的的InputStream 从这个文件读取和写入到你的 DataOutputStream类

替代流是使用一些其他的类,在相同的方法,因为流基地,它们分别是:JsonWriter / JsonReader,但你也有FileWriter的/的FileReader如果它不是具体的Json你传递什么,不要别忘了经常换那些的BufferedReader和BufferedWriter将。

I am trying to send multiple Json string[each 1 MB] at a time to PHP Server. When i tried to send 10 records its working fine. But if it exceeds 20 records it says OutOfMemoryException. I seen that android memory size limit t0 15MB - 16MB. But haven't got any clue how to resolve this, I am using the below code to send multiple records at a time.

 /** Upload Data*/
private void submitUploadData(String url, Map<String, String> param)
        throws IOException {
    URL siteUrl;
    try {
        siteUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) siteUrl
                .openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        String content1 = ""; 

        Set getkey = param.keySet();
        Iterator keyIter = getkey.iterator();
        String content = "";
        for (int i = 0; keyIter.hasNext(); i++) {
            Object key = keyIter.next();
            if (i != 0) {
                content += "&"; //Crashing here
            }
            content += key + "=" + param.get(key); //Crashing here
            System.out.println("Content" + content);
        }

        System.out.println(content);
        out.writeBytes(content.trim());
        out.flush();
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String jsonresponse = "";
        while ((jsonresponse = in.readLine()) != null) {
            System.out.println(jsonresponse);   
            if(!jsonresponse.equals("Authorisation Successful")){
            updateUploadRecords(jsonresponse);}

        }
        in.close();  

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } 
}

解决方案

at first glance there's nothing wrong with your code but you're using every possible type of method that is known for being a memory waster and now that you're trying large operations you running out of memory, that was guaranteed to happen.

tips:

  • do not contatenate strings with str1 += str2 + "other val" you just created 3 new strings in memory. Use a StringBuilder instead.
  • Calling System.gc() (as some will suggest you) will not help you, because you're in a Loop doing this operation and that method only hints the system that it might be a good time to GC, but until the system actually get around to do it, you'll have crashed already.
  • If each JSON string is around 1mb and you're passing 20 of them, you're already crashing with out of memory on older devices right there with those 20mb. You'll have to re-design quite a few stuff, but my suggestion is to just remove the Map, completely remove it.

Instead you should (on the method that is creating that map) to use some type of OutputStreamto write all that data, already formatted in a temporary file. Then, to upload you'll use some type of InputStream to read from this file and write to your DataOutputStream

Alternative to the Streams is to use some other classes that bases on the same methodology as streams, they are: JsonWriter/JsonReader, but then you also have FileWriter/FileReader if it's not specific Json what you're passing, and don't forget to always wrap those with BufferedReader and BufferedWriter.

这篇关于OutOfMemoryException异常,而试图大JSON数据发送到服务器的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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