Reputation: 2494
I need to get large amount of data (say 7000 records) from online database to Android application. While searching the web, I came to know that large amount of data will leads to outofmemory problem in json while convert from string. The solution was to convert the json with the help of gson or jackson. I have two question as follows
Another solution for converting json to avoid out of memory problem.
Shall I get the data in xml format? If so whether I can solve out of memory?
String result = convertStreamToString(is);
JSONObject jObject = new JSONObject(result); // Only i am getting outofMemory Exception..
private static String convertStreamToString(InputStream inputStream)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1024];
int n = 0;
try {
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
} finally {
out.close();
in.close();
}
return out.toString("UTF-8");
}
Upvotes: 2
Views: 1623
Reputation: 30168
You should use Jackson
, since it's a streaming processor, you can directly feed in an arbitrarily sized InputStream
to the ObjectMapper.readValue()
method. As long as the records themselves are not too heavy, then you should be able to keep them in memory. That said, you should probably not keep them in memory, you should feed them into a database and query them from there.
Upvotes: 0
Reputation: 29632
I think xml format will allow you for large amount of data. I am developing a Project where I download approx 15000 data in xml and there is no a memory issue.
Upvotes: 1