java - Outofbound memory exception while downloading data from server in android -
i have download around 6000 records sqlite database server. records present json
objects. while retrieving them using bufferedreader
, error out of memory bound exception. have searched many sites not find appropriate answer. please suggest best solution.
my code is:
url url = new url("myurl"); bufferedreader in = new bufferedreader(new inputstreamreader(url.openstream())); //getting error here line = in.readline(); system.out.println(line); jsonarray outerarray = (jsonarray) jsonserializer.tojson(line); system.out.println("size : " + outerarray.size());
i got fatal exception -
e/androidruntime(616): java.lang.outofmemoryerror: [memory exhausted]
size of downloading file hold in memory
try doing this
// download file inputstream input = new bufferedinputstream(url.openstream()); outputstream output = new fileoutputstream(); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... output.write(data, 0, count); } output.flush(); output.close(); //
for more explanation see this
also
you should use json streaming either gson or jackson. jackson can use hybrid approach well. reduce memory consumption portion of json being parsed loaded memory.
Comments
Post a Comment