Java文件流关闭和垃圾回收问题

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

周末碰到一段代码,是关于Java IO流的,代码中出现在一个多线程的系统中,其中一段代码在打开一个文件操作流用完以后没有及时关闭,开始以为很快会出现打开文件太多或者导致内存溢出,但是在运行了很长时间以后仍然没有出现问题,后来自己写了个小程序测试了一下,总算似乎搞清楚为啥了。

先看以下一段代码

  1. import java.io.FileInputStream;  
  2. public class TTT {  
  3.     public static void main(String[] args) throws Exception {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             final String threadId = "thread_" + i;  
  6.             Thread thread = new Thread(new Runnable() {  
  7.                 public void run() {  
  8.                     System.out.println(threadId + " started!");  
  9.                     try {  
  10.                         FileInputStream fis = new FileInputStream("/opt/test.log");  
  11.                         Thread.sleep(60 * 1000);  
  12.                     } catch (Exception ex) {  
  13.                         ex.printStackTrace();  
  14.                     }  
  15.                     System.out.println(threadId + " stopped!");  
  16.                 }  
  17.             });  
  18.             thread.start();  
  19.         }  
  20.         Thread.sleep(10 * 60 * 1000);  
  21.     }  
  22. }  
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); }}

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

[python] view plain copy print ?
  1. $ /usr/sbin/lsof -p `ps -ef | grep java | grep TTT | awk '{print $2}'` | grep "test.log"  
  2. java    21562 fkong    3r   REG  253,0        0 35471424 /opt/test.log  
  3. java    21562 fkong    4r   REG  253,0        0 35471424 /opt/test.log  
  4. java    21562 fkong    5r   REG  253,0        0 35471424 /opt/test.log  
  5. java    21562 fkong    6r   REG  253,0        0 35471424 /opt/test.log  
  6. java    21562 fkong    7r   REG  253,0        0 35471424 /opt/test.log  
  7. java    21562 fkong    8r   REG  253,0        0 35471424 /opt/test.log  
  8. java    21562 fkong    9r   REG  253,0        0 35471424 /opt/test.log  
  9. java    21562 fkong   10r   REG  253,0        0 35471424 /opt/test.log  
  10. java    21562 fkong   11r   REG  253,0        0 35471424 /opt/test.log  
  11. java    21562 fkong   12r   REG  253,0        0 35471424 /opt/test.log  
$ /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.logjava    21562 fkong    4r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong    5r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong    6r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong    7r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong    8r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong    9r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong   10r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong   11r   REG  253,0        0 35471424 /opt/test.logjava    21562 fkong   12r   REG  253,0        0 35471424 /opt/test.log 

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

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

  1. import java.io.FileInputStream;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. public class TTT {  
  5.     public static void main(String[] args) throws Exception {  
  6.         List<Thread> threads = new ArrayList<Thread>();  
  7.         for (int i = 0; i < 10; i++) {  
  8.             final String threadId = "thread_" + i;  
  9.             Thread thread = new Thread(new Runnable() {  
  10.                 public void run() {  
  11.                     System.out.println(threadId + " started!");  
  12.                     try {  
  13.                         FileInputStream fis = new FileInputStream("/opt/test.log");  
  14.                         Thread.sleep(60 * 1000);  
  15.                     } catch (Exception ex) {  
  16.                         ex.printStackTrace();  
  17.                     }  
  18.                     System.out.println(threadId + " stopped!");  
  19.                 }  
  20.             });  
  21.             thread.start();  
  22.             threads.add(thread);  
  23.         }  
  24.         Thread.sleep(2 * 60 * 1000);  
  25.         for (Thread thread : threads) {  
  26.             thread = null;  
  27.         }  
  28.         System.out.println("Clean up threads!");  
  29.         Thread.sleep(10 * 60 * 1000);  
  30.     }  
  31. }  
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操作,如下:

  1. import java.io.FileInputStream;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. public class TTT {  
  5.     public static void main(String[] args) throws Exception {  
  6.         List<Thread> threads = new ArrayList<Thread>();  
  7.         for (int i = 0; i < 10; i++) {  
  8.             final String threadId = "thread_" + i;  
  9.             Thread thread = new Thread(new Runnable() {  
  10.                 public void run() {  
  11.                     System.out.println(threadId + " started!");  
  12.                     try {  
  13.                         FileInputStream fis = new FileInputStream("/opt/test.log");  
  14.                         Thread.sleep(60 * 1000);  
  15.                     } catch (Exception ex) {  
  16.                         ex.printStackTrace();  
  17.                     }  
  18.                     System.out.println(threadId + " stopped!");  
  19.                 }  
  20.             });  
  21.             thread.start();  
  22.             threads.add(thread);  
  23.         }  
  24.         Thread.sleep(2 * 60 * 1000);  
  25.         for (Thread thread : threads) {  
  26.             thread = null;  
  27.         }  
  28.         System.out.println("Clean up threads!");  
  29.           
  30.         System.gc();  
  31.         System.gc();  
  32.         System.gc();  
  33.         System.out.println("Finished GC!");  
  34.           
  35.         Thread.sleep(10 * 60 * 1000);  
  36.     }  
  37. }  
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去干这些事总归还是不好的,还是那句老话,自己的事情自己做。

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/mm2zzyzzp/article/details/84038629