Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Out of Memory parsing base64 string fromJSON

Getting an out of memory error parsing a large JSON string.

I've seen several examples of using GSON to stream large JSON documents, but my problem is that my JSON document does not have hundreds or thousands of 'rows' which can be tokenized efficiently. Rather it contains a single base64 node with around 15MB of data, e.g.

{
    "id":"somefile",
    "base64":"super long base64 string...."
}

How can I parse the single base64 string from this JSON document into a file and save to the file system without running out of memory? Parsing the base64 node into a String using StringBuilder is what's causing the current OOM error. e.g.

...
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    sb.append(line); //out of memory
}
reader.close();
//parse the response into a POJO
MyFileClass f = new Gson().fromJson(sb.toString(), MyFileClass.class);
like image 362
rmirabelle Avatar asked Dec 18 '25 12:12

rmirabelle


1 Answers

Looks like I've found a solution. It appears that there's no need to parse the JSON into a String before using GSON. You can pass the InputStream directly to GSON as in the following example (compare to the code in the question):

InputStream is = con.getInputStream();
BufferedReader buff_reader = new BufferedReader(new InputStreamReader(is));
//use GSON to create a POJO directly from the input stream
MyType instance = new Gson().fromJson(buff_reader, MyType.class);

I'm not entirely certain whether using a BufferedReader is necessary, because the fromJSON method will accept an InputStreamReader directly, but it seems like a good idea.

like image 158
rmirabelle Avatar answered Dec 21 '25 05:12

rmirabelle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!