开发者

Out of memory error when putting large JSON (InputStream) to String

开发者 https://www.devze.com 2023-03-02 11:14 出处:网络
I receive gziped JSON from web service and then i unzip it (size of unziped JSON is 3.2MB). I need to transform received InputStream to String so i can then create JSONObject and parse it. I do it wi

I receive gziped JSON from web service and then i unzip it (size of unziped JSON is 3.2MB). I need to transform received InputStream to String so i can then create JSONObject and parse it. I do it with this code:

public static String InputStreamToString(InputStream in) 
    throws IOException {

    BufferedInputStream bis = new BufferedInputStream(in);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = b开发者_Python百科is.read();

    while(result != -1) {
      byte b = (byte)result;
      buf.write(b);
      result = bis.read();
    }        
    return buf.toString();
}

I receive java.lang.OutOfMemoryError on the last line: "return buf.toString();" on the emulator and device with 288MB Ram.

What shall i do?


Reading in a byte at a time is so 1990's. Either use HttpClient and BasicResponseHandler, or at least read the data in respectable chunks and append them using a StringBuilder.

Assuming you are still having the problem, the issue is that there is no single block of memory that is big enough for your string, based upon other things your app has been doing. The Android garbage collector is not a compacting collector, so it is possible to have lots of free heap space yet not enough for a specific allocation request.

In that case, you may need to switch to some sort of streaming JSON parser. If you happen to be targeting only Honeycomb and higher, you can use JSONReader. Otherwise, Jackson reportedly works on Android and apparently has a streaming mode.


You can try to create a new JSONObject using

new JSONObject(new JSONTokener(in)) 

instead of converting in to a String directly. However, this will probably only delay the problem. If you don't have enough memory to load a 3.2 meg string into memory, you probably won't have enough memory to load that as a json object, which will take more memory than the simple string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号