问题
I am working on http get request on android.
I receive a huge amount of json data from server, a string is unable to handle or store that data and getting OutOfMemory Exception.
Then I tried to save it in arrayList which can store a maximum of Integer.Maximum value. but it is getting OutOfMemory exception while storing at ~8970 location.
Here is my link which has json data.
http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist10122013035042.txt
here is my code:
ArrayList<String> newarr = new ArrayList<String>();
try {
URL url = new URL(urlFilePath);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
// urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
int check;
while ((bufferLength = inputStream.read(buffer)) > 0) {
String decoded = new String(buffer, 0, bufferLength);
newarr.add(decoded); // OutOfMemory Exception.
}
fileOutput.close();
buffer = null;
inputStream.close();
String path = file.getAbsolutePath();
return path;
} catch (final MalformedURLException e) {
e.printStackTrace();
return "";
} catch (final IOException e) {
e.printStackTrace();
return "";
} catch (final Exception e) {
System.out.println("EXCEPTION:: " + e.getMessage());
e.printStackTrace();
return "";
}
回答1:
You need to process the stream directly instead of storing it as a String. Look at the Gson Stream Reader as an example:
public List<Message> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
List<Message> messages = new ArrayList<Message>();
reader.beginArray();
while (reader.hasNext()) {
Message message = gson.fromJson(reader, Message.class);
messages.add(message);
}
reader.endArray();
reader.close();
return messages;
}
You can think of the List here as the parsed result. You could also use the list from whatever ListAdapter
you are using if you want to display it.
回答2:
the maximum capacity of an ArrayList (or any kind of list whatsoever) is limited only by the amount of memory the JVM has available.
U can use BufferedReader, or maybe a Scanner instead of String.
Else if u still want to use String then try to increase the JVM.As far as i know there is no way of controlling the heap size at runtime.
It may not be necessary though: you can provide a minimum and maximum heap size with the -Xms and -Xmx switches respectively. (eg -Xms128m -Xmx512m) The jvm will manage the actual heap size within these bounds.
Try to set the limit according to your usage
For more refference try to get some idea from
http://www.coderanch.com/t/614644/Performance/java/Arraylist-heapsize-memory-error
来源:https://stackoverflow.com/questions/20519858/outofmemory-error-in-arraylist-android