Java文件流關(guān)閉和垃圾回收機制
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命令查看的結(jié)果都一樣,都可以看到有10個文件流沒有關(guān)閉。
3.下面我把這個代碼做了一些改動,就是在線程執(zhí)行完之后,將所有線程置為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查看,結(jié)果仍然類似,還是有10個文件流沒有關(guān)閉。
我再次做了一些改動,在將所有線程置為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!”之后,看到的結(jié)果是那10個打開的文件流都被關(guān)閉了。
最后,我干脆把那些設(shè)置thread為null的語句刪除了,運行的結(jié)果也和上面執(zhí)行g(shù)c操作的結(jié)果一致。
最終,JVM中對于那些打開了沒有關(guān)閉的IO文件流,會在不再被使用的情況下,等到下次做Full GC的時候把他們?nèi)炕厥?,但是讓JVM去干這些事總歸還是不好的,還是那句老話,自己的事情自己做。
相關(guān)文章
Java學(xué)習(xí)之反射機制及應(yīng)用場景介紹
本篇文章主要介紹了Java反射機制及應(yīng)用場景,反射機制是很多Java框架的基石。非常具有實用價值,需要的朋友可以參考下。2016-11-11淺談Spring中@Transactional事務(wù)回滾及示例(附源碼)
本篇文章主要介紹了淺談Spring中@Transactional事務(wù)回滾及示例(附源碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤
這篇文章主要給大家介紹了關(guān)于IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤的解決辦法,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01java+jdbc+mysql+socket搭建局域網(wǎng)聊天室
這篇文章主要為大家詳細(xì)介紹了java+jdbc+mysql+socket搭建局域網(wǎng)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Springboot通過請求頭獲取當(dāng)前用戶信息方法詳細(xì)示范
這篇文章主要介紹了Springboot通過請求頭獲取當(dāng)前用戶信息的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時候報decompiled.class file byt
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時候報decompiled.class file bytecode version:52.0(java 8)錯誤的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10