apache.commons.io.IOUtils throw out of memory error

2.3k Views Asked by At

I'm using the following code to get the string representation of server response. The problem is that it's throwing an out of memory exception when it tries to pass the response toString(). This code is running on Android.

String resp = IOUtils.toString(resEntity.getContent(), "UTF-8");

HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("Accept", "JSON");

HttpResponse httpResponse = httpclient.execute(httpGet);
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
  //String resp = EntityUtils.toString(resEntity);
  String resp = IOUtils.toString(resEntity.getContent(), "UTF-8"); // exception here
  Log.i(TAG, resp);
// ........
// gosn parser code here
// ........
}

The stacktrace is:

java.lang.OutOfMemoryError: (Heap Size=47395KB, Allocated=36544KB)
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
at java.lang.StringBuilder.append(StringBuilder.java:271)
at org.apache.commons.io.output.StringBuilderWriter.write(SourceFile:138)
at org.apache.commons.io.IOUtils.copyLarge(SourceFile:2002)
at org.apache.commons.io.IOUtils.copyLarge(SourceFile:1980)
at org.apache.commons.io.IOUtils.copy(SourceFile:1957)
at org.apache.commons.io.IOUtils.copy(SourceFile:1907)
at org.apache.commons.io.IOUtils.toString(SourceFile:778)
at org.apache.commons.io.IOUtils.toString(SourceFile:803)
0

There are 0 best solutions below