源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java文件流关闭和垃圾回收机制

  • 时间:2021-07-12 05:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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 <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去干这些事总归还是不好的,还是那句老话,自己的事情自己做。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部