stringbuffer.tostring caused by Java heap space

Today in the test "Generate Report" feature, the emergence of this problem, java throws java.lang.OutOfMemoryError: Java heap space:

Because the development is the use of tomcat unified configuration, and the rest of the file generation function properly, so the exclusion of jvm configuration, according to the java log, or find code issues

StringBuffer buf=new StringBuffer();
FileOutputStream fos=null;
BufferedWriter bw=null;
while(if((temp=br.readLine())!=null)){
        buf.append(temp);
        buf.append("\n");
}        
buf.append (filein);
fos=new FileOutputStream(file);
OutputStreamWriter osw=new OutputStreamWriter (fos,"UTF-8");
bw = new BufferedWriter (Abou);
bw.write(buf.toString());
bw.flush();

object.close()...

These are part of the code, the problem bw.write (buf.toString ()); the buf.toString (), due to the large filein string, after splicing buf toString () heap overflow

The solution is to set a threshold that, when filein size reaches a critical value, do a write operation, due to the segmentation write,

= new new fos a FileOutputStream (File, to true); append mode change.

Guess you like

Origin www.cnblogs.com/kaizhengMan/p/11098988.html