1

I'm using a soap service on android device and as response I get 7mb file from server. This causes my app to crash with out of memory error when converting input stream to string. MemoryAnalyzer shows that memory was allocated to StreamBuilder. What is the best way to deal with such big responses?

HttpEntity entity = new StringEntity(soap.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
if( r_entity != null ) {
   result = inputStreamToString(r_entity.getContent());
}
...
//convert stream to string
public static String inputStreamToString(InputStream stream) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
          sb.append(line + "\n");
    }
    br.close();
    return sb.toString();
}
4
  • 1
    You'll have to ask the question why is it so big.. Strings can be memory expensive, how come you need so much? Commented May 16, 2012 at 15:04
  • why dont u read single byte and save in file???? Commented May 16, 2012 at 15:06
  • Agree with Blundell. Instead of trying to find a way to read a 7mb xml I'd think of a way to make that xml smaller. there's probably something you can do to break that response in smaller parts. Commented May 16, 2012 at 15:13
  • This is the reply from SAP web service, I have no influence on the amount of data I get and I cannot change the web service. Commented May 16, 2012 at 20:16

2 Answers 2

1

The most obvious answer is to use streams and parse the result as it comes. If it's an XML file you're parsing then SAX is probably the best avenue for you.

Sign up to request clarification or add additional context in comments.

4 Comments

I am already using SAX parser, but I was previously converting input stream to string. I tried now SAX parser with bufferedreader but it throws IOException, hm....
What does the Exception say ?
It said something about missing ending. I tried it again now and I cannot reproduce the error. Strange because I always get the same response. Anyway I decided to use InputStream directly with SAX, without BufferedReader: codeXml.parse(result, Encoding.UTF_8, unitParser);code Thanks for your help
Exception said unclosed token
0

StringBuilder requires twice as much memory as it has data at the time of expansion. A new, larger (almost for sure larger than required) array of chars will be allocated and a copy will be made, only then the old array can be discarded.

Collect all lines into ArrayList<String> that is cheaper to grow, just a size of String reference per line. After you finish reading, you can count exact length in a loop and allocate StringBuilder of exactly required size. Close the stream first. Or, maybe you can reuse the list directly without converting into string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.