是否会导致资源泄漏,将流对象设置为null但不明确关闭它?

时间:2015-08-20 08:28:21

标签: java memory-leaks io stream destructor

FileReader f = new FileReader("foo.txt");
// Now there is a file descriptor (or file handle) created by OS.
f = null;
// Please note: we don't call f.close(), will the file handle
// be closed automatically?

它会导致Java中的资源(文件描述符/文件句柄)泄漏吗?

1 个答案:

答案 0 :(得分:2)

这是重复:Do unclosed streams cause memory leaks in java?

简短的回答是:不是真的。流将由垃圾收集器完成。但是你不知道什么时候会发生这种情况。所以你浪费了一些时间的记忆。