Search code examples
javastringbuffer

String buffer - OutOfMemoryError


I am having string buffer variable which holds the input i read from a file. Some cases i am getting input file in huge size. In those cases i am getting the OutOfMemoryError.

Here is my code:

StringBuffer response = new StringBuffer("");
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
        response.append(inputLine);
in.close();

Kindly help me how to resolve this issue.


Solution

  • Either read the input in chunks, or change the -Xmx parameter (maximum memory size) in your JVM to a larger size.