Very Large Soap Response - Android- Out of Memory Error

Very large SOAP response - Android- out of memory error

Two strategies to help you solve this problem:

  1. Save your SOAP XML stream directly to disk as you download it. Don't store it in memory.
  2. Parse it using a SAX-style parser, where you don't load the whole DOM in memory, but rather parse it in chunks.

Depending on the kind of XML you are handling, using SAX parsers is usually harder in code; you will have to keep track of many things yourself, and you won't be able to "jump" from section to section of your DOM tree. But the memory consumption will be way lower.

Take note, however, that many "high-level" network communication libraries usually load the whole XML DOM in memory, which might be the case here. You will probably have to create and manage the HTTP connection yourself, and then manually parse the result.

SOAP - Very large XML response - OutOfMemoryError

Thank's Graeme, opening connection as a byte stream works. If it could be helpfull to somebody, this is my source code

        //Make a temporary file to save data
File responseFile = File.createTempFile("SOAP", "xml", context.getFilesDir());
int nbCharRead = 0; int i=0; int totalRead = 0;

//Send query
OutputStream outputS = url_Connection.getOutputStream();
Writer w_out = new OutputStreamWriter(outputS);
w_out.write(webServiceXml);
w_out.flush();
w_out.close();

//Buffers
BufferedReader bufReader = new BufferedReader(new InputStreamReader(url_Connection.getInputStream()));
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(responseFile));
char[] buffer = new char[10000];

while((nbCharRead = bufReader.read(buffer, 0, 10000)) != -1)
{
totalRead += nbCharRead;
Log.d("Test InputStream", "i: " + i++ +" - " + nbCharRead + " -> " + totalRead);
bufWriter.write(buffer, 0, nbCharRead );
}

if(bufWriter != null)
{
bufWriter.flush();
bufWriter.close();
}

Log.w(MsgLog, "--- Stream Got--- ; Total : " + totalRead);

Getting out of memory error while reading responce from web service

For all the variables available in the while ((result=rd.readLine()) != null) loop, the memory will be created on the Heap, as you mentioned you are trying to stream aroung 1 MB data, loop will exceute long time so obivously the heap has become overloaded to store the data.

My suggestions:

  1. Try to stream minimum amount of data and check
  2. Avoid having Instantiation of objects inside loop, use references instead.
  3. Find a way to increase the JVM memory size

Links to consider are:

Increase heap size in java

JVM-Java increase heap size



Related Topics



Leave a reply



Submit