Java文件流关闭和垃圾回收机制
程序员文章站
2024-03-12 21:24:15
1.先看以下一段代码
import java.io.fileinputstream;
public class ttt {
public static vo...
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 <pid>来查看这个程序打开的文件信息
$ /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<thread> threads = new arraylist<thread>(); 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<thread> threads = new arraylist<thread>(); 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去干这些事总归还是不好的,还是那句老话,自己的事情自己做。