1

I am trying to call a web service, the response contains a big string, getting out of memory issue the code is

 URL u = new URL(SharedVariables.server);
           URLConnection uc = u.openConnection();
           HttpURLConnection connection = (HttpURLConnection) uc;
           connection.setConnectTimeout(SharedVariables.connectionTimedOutValue);
           connection.setDoOutput(true);
           connection.setDoInput(true);
           connection.setRequestProperty("SOAPAction", SOAP_ACTION);
           connection.setRequestMethod("POST");
           connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");

           String xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
                            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> "+ 
                            "<soap:Body>"+
                            "<GetCaseCriminalTicketLinks xmlns=\"http://tempuri.org/\">"+   
                            Req.getRequestXml()+                            
                            "</GetCaseCriminalTicketLinks>"+
                            "</soap:Body>"+
                            "</soap:Envelope>";        

           OutputStream out = connection.getOutputStream();
                Writer wout = new OutputStreamWriter(out);
                wout.write(xmldata);
                wout.flush();                   
                out.flush();                    


              BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()),8 * 1024);

              String result;    

              while ((result=rd.readLine()) != null) {


              int length = result.length();               
               String temp = result.substring(316, (length - 100));           

               JSONObject tempJson = new JSONObject(temp);}

The log is

Caused by: java.lang.OutOfMemoryError
at java.lang.String.<init>(String.java:513)
at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:650)
at java.lang.StringBuilder.toString(StringBuilder.java:664)
at java.io.BufferedReader.readLine(BufferedReader.java:398)

this line is causing

while ((result=rd.readLine()) != null) 

Any suggestions ?

Thanks

Kishore
  • 2,051
  • 5
  • 26
  • 45

1 Answers1

0

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

Community
  • 1
  • 1
Murugesh
  • 1,009
  • 1
  • 12
  • 34