java 能不能回收 文件流_Java文件流关闭和垃圾回收机制

1.先看以下一段代码

import java.io.FileInputStream;

public class TTT {

public static void main(String[] args) throws Exception {

for (int i = 0; i < 10; i++) {

final String threadId = "thread_" + i;

Thread thread = new Thread(new Runnable() {

public void run() {

System.out.println(threadId + " started!");

try {

FileInputStream fis = new FileInputStream("/opt/test.log");

Thread.sleep(60 * 1000);

} catch (Exception ex) {

ex.printStackTrace();

}

System.out.println(threadId + " stopped!");

}

});

thread.start();

}

Thread.sleep(10 * 60 * 1000);

}

}

2.在linux上编译并运行这个类,然后使用linux的命令/usr/sbin/lsof -p 来查看这个程序打开的文件信息

$ /usr/sbin/lsof -p `ps -ef | grep java | grep TTT | awk '{print $2}'` | grep "test.log"

java 21562 fkong 3r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 4r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 5r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 6r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 7r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 8r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 9r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 10r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 11r REG 253,0 0 35471424 /opt/test.log

java 21562 fkong 12r REG 253,0 0 35471424 /opt/test.log

不管是在10个线程运行过程中还是运行完,使用lsof命令查看的结果都一样,都可以看到有10个文件流没有关闭。

3.下面我把这个代码做了一些改动,就是在线程执行完之后,将所有线程置为null,如下

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

public class TTT {

public static void main(String[] args) throws Exception {

List threads = new ArrayList();

for (int i = 0; i < 10; i++) {

final String threadId = "thread_" + i;

Thread thread = new Thread(new Runnable() {

public void run() {

System.out.println(threadId + " started!");

try {

FileInputStream fis = new FileInputStream("/opt/test.log");

Thread.sleep(60 * 1000);

} catch (Exception ex) {

ex.printStackTrace();

}

System.out.println(threadId + " stopped!");

}

});

thread.start();

threads.add(thread);

}

Thread.sleep(2 * 60 * 1000);

for (Thread thread : threads) {

thread = null;

}

System.out.println("Clean up threads!");

Thread.sleep(10 * 60 * 1000);

}

}

再次在10个线程运行过程中和运行完毕后使用lsof查看,结果仍然类似,还是有10个文件流没有关闭。

我再次做了一些改动,在将所有线程置为null以后,增加(或者说是催促JVM)做几次gc操作,如下:

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

public class TTT {

public static void main(String[] args) throws Exception {

List threads = new ArrayList();

for (int i = 0; i < 10; i++) {

final String threadId = "thread_" + i;

Thread thread = new Thread(new Runnable() {

public void run() {

System.out.println(threadId + " started!");

try {

FileInputStream fis = new FileInputStream("/opt/test.log");

Thread.sleep(60 * 1000);

} catch (Exception ex) {

ex.printStackTrace();

}

System.out.println(threadId + " stopped!");

}

});

thread.start();

threads.add(thread);

}

Thread.sleep(2 * 60 * 1000);

for (Thread thread : threads) {

thread = null;

}

System.out.println("Clean up threads!");

System.gc();

System.gc();

System.gc();

System.out.println("Finished GC!");

Thread.sleep(10 * 60 * 1000);

}

}

再次使用lsof查看,在运行中仍然还是可以看到那有10个文件流打开着,但是在“Finished GC!”之后,看到的结果是那10个打开的文件流都被关闭了。

最后,我干脆把那些设置thread为null的语句删除了,运行的结果也和上面执行gc操作的结果一致。

最终,JVM中对于那些打开了没有关闭的IO文件流,会在不再被使用的情况下,等到下次做Full GC的时候把他们全部回收,但是让JVM去干这些事总归还是不好的,还是那句老话,自己的事情自己做。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值