我相信开放流会导致java中的内存泄漏(至少java 1.6及更早版本确实存在这个问题).
但是,在搜索时(即使在这里),我发现有些人同意这一点,而其他人却没有.所以,如果我写这个程序:
import java.io.*;
public class CreatingMemoryLeak {
public static void main(String args[])
{
String s = "xxxxxxx";
InputStream in = new ByteArrayInputStream(ss.getBytes());
BufferedInputStream bf = new BufferedInputStream(in);
try {
while(bf.read()>0)
{
System.out.println("got it");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Here is a input stream " + s +" causing a memory leak");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我没有bf明确关闭流,它会导致内存泄漏吗?