This discussion is archived
2 Replies Latest reply: Sep 22, 2006 7:14 AM by 807598 RSS

stringbuffer memory problem

807598 Newbie
Currently Being Moderated
Hi guys

I am developing an applet which reads a 25mb text file into a stringbuffer and outputs the stringbuffer content to a text area. the file is an auto generated report.
File f = new File ("c:\\my_file.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();          
String oLine;     
while ((oLine = br.readLine()) != null) {
     sb.append(oLine+"\n");
}
br.close();               
TextArea data = new TextArea(sb.toString(),50,110);
This works fine when I increase my JVM size to 128mb. But runs out of memory when using the default JVM size.

Is there a more efficient way I can do this so that it works in default JVM memory size? A datatype that takes up less memory than stringbuffer perhaps?

Thanks in advance.
  • 1. Re: stringbuffer memory problem
    800322 Newbie
    Currently Being Moderated
    The question is: what are 25 MB of text doing in an applet? Who's supposed to look at all that info? Trim down the amount of information you display. You couldn't even navigate through that pile of text.
    A datatype that takes up less memory than stringbuffer perhaps?
    How would that work? If you have 25 MB of data, you'll have 25 MB of data.

    I guess your problem comes up when you create a String from the StringBuffer - I don't know if or how StringBuffer and String are sharing their backing arrays, but I'd assume for simplicity's sake they don't. So you end up with a 25 MB String and a 25 MB StringBuffer.
  • 2. Re: stringbuffer memory problem
    807598 Newbie
    Currently Being Moderated
    Just a thought but could you not simply append and output the text to the text area of the applet in increments instead of all at once, and then clear the StringBuffer and repeat for the next portion of text from the file? It would probably take longer to display the text due to its massive size but you wouldn't run out of memory for the StringBuffer object itself.