JavaBean:
package memory;
import java.io.*;
public class OutOfMemoryBean2{
private FileInputStream fin = null;
StringBuffer strBu = null;
public OutOfMemoryBean2(){
try {
fin = new FileInputStream("E:\\test.txt");
}catch (Exception ex) {
System.out.println(ex.toString());
}
strBu = new StringBuffer();
}
public String getFile(){
int ch = 0;
try {
while((ch = fin.read()) != -1){
strBu.append((char)ch);
}
fin.close();
}catch (Exception ex) {
System.out.println(ex.toString());
}
return strBu.toString();
}
public static void main(String[] args){
OutOfMemoryBean2 outOf2 = new OutOfMemoryBean2();
String str = outOf2.getFile();
System.out.println(str);
}
}
test.txt 文件是比较大的文件,在jsp中生成上面类的对象,调用 getFile().
在 Tomcat 下会不会由于Tomcat主线程的存在,每
一次刷新页面都生成一个新的字符串,而以前的字符串还在消耗内存?我在做实验时刚
开始内存在增加,但增加到一定程后不再增加或增加的很少。java 在释放空间时是不是
需要多少内存就释放多少?我产生若干个这个页面后,服务会不会死掉。