关于守护线程不能持有需要关闭的资源的疑问。
Topic sourcepublic class Test05 {
public static void main(String[] args) throws IOException {
for (int i = 0; i < 10000000; i++) {
System.out.println("加载次数:" + i);
InputStream is = new FileInputStream("D:\\person.xml");
is.close();
}
}
}
public class Test05 {
public static void main(String[] args) throws IOException {
for (int i = 0; i < 10000000; i++) {
System.out.println("加载次数:" + i);
InputStream is = new FileInputStream("D:\\person.xml");
}
}
}
未加close方法,通过电脑任务管理器看到,idea内存一直在增加,不过每次好像都只加0.1m
加了 close 方法 idea 内存达到固定值 基本上就不变了
参考地址Java InputStream未调用close方法进行资源关闭造成直接内存泄漏_coldice0521的博客-CSDN博客_inputstream不关闭会怎样
总算是有点明白了,还是有点不明白资源丢失是什么意思,也有有一天会明白的
- 1
小样_这么多人抢
如果用try-catch-finally或者Try-with-resources能自行关闭资源吗?