Java文件流關(guān)閉和垃圾回收機(jī)制
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上編譯并運(yùn)行這個(gè)類,然后使用linux的命令/usr/sbin/lsof -p <pid>來查看這個(gè)程序打開的文件信息
$ /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個(gè)線程運(yùn)行過程中還是運(yùn)行完,使用lsof命令查看的結(jié)果都一樣,都可以看到有10個(gè)文件流沒有關(guān)閉。
3.下面我把這個(gè)代碼做了一些改動(dòng),就是在線程執(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個(gè)線程運(yùn)行過程中和運(yùn)行完畢后使用lsof查看,結(jié)果仍然類似,還是有10個(gè)文件流沒有關(guān)閉。
我再次做了一些改動(dòng),在將所有線程置為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查看,在運(yùn)行中仍然還是可以看到那有10個(gè)文件流打開著,但是在“Finished GC!”之后,看到的結(jié)果是那10個(gè)打開的文件流都被關(guān)閉了。
最后,我干脆把那些設(shè)置thread為null的語句刪除了,運(yùn)行的結(jié)果也和上面執(zhí)行g(shù)c操作的結(jié)果一致。
最終,JVM中對(duì)于那些打開了沒有關(guān)閉的IO文件流,會(huì)在不再被使用的情況下,等到下次做Full GC的時(shí)候把他們?nèi)炕厥?,但是讓JVM去干這些事總歸還是不好的,還是那句老話,自己的事情自己做。
相關(guān)文章
Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能示例
這篇文章主要介紹了Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能,結(jié)合實(shí)例形式分析了java使用socket進(jìn)行多線程數(shù)據(jù)傳輸?shù)南嚓P(guān)操作技巧,需要的朋友可以參考下2017-08-08
IDEA上面搭建一個(gè)SpringBoot的web-mvc項(xiàng)目遇到的問題
這篇文章主要介紹了IDEA上面搭建一個(gè)SpringBoot的web-mvc項(xiàng)目遇到的問題小結(jié),需要的朋友可以參考下2017-04-04
java web實(shí)現(xiàn)用戶權(quán)限管理
這篇文章主要介紹了java web實(shí)現(xiàn)用戶權(quán)限管理,設(shè)計(jì)并實(shí)現(xiàn)一套簡(jiǎn)單的權(quán)限管理功能,感興趣的小伙伴們可以參考一下2015-11-11
SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法
本文主要介紹了SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解
我們?cè)贘ava編程中經(jīng)常碰到類型轉(zhuǎn)換,對(duì)象類型轉(zhuǎn)換主要包括向上轉(zhuǎn)型和向下轉(zhuǎn)型,這篇文章主要介紹了Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
使用bitset實(shí)現(xiàn)毫秒級(jí)查詢(實(shí)例講解)
下面小編就為大家?guī)硪黄褂胋itset實(shí)現(xiàn)毫秒級(jí)查詢(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

