原則:最好在任何時候使用InputStream或者OutputStream的時候,在finally中調用close()方法,顯式關閉。
一個典型的示例
InputStream in = null;
try {
in = acquireStream();
...
} finally {
if (in != null) in.close();
}
fianlly
中的if (in != null) in.close();
也可以用IOUtils.closeQuietly(in);
代替,需要Apache Commons-IO
。
為什么需要調用
InputStream的作用是用來表示從不同數據源產生輸入的類,這些數據源包括:
- 字節數組
- String對象
- 文件
- 管道
- 一個由其他種類的流組成的序列,以便我們可以將他們收集合並到一個流內。
- 其他數據源,如Internet連接等
通常不使用close會導致內存泄露,垃圾回收機制會回收,但是最好自己顯式關閉,這並不是特別關鍵。
關鍵是當InputStream的數據源是文件或者Internet連接的時候。
OutputStream的作用是如FileOutStream,當不調用close的時候,不會將緩存刷入文件中。
InputStream的數據源是文件時
一個InputStream會韓勇一個極小的kernel資源,一個低級的file handle。
當打開文件時,將文件讀入到InputStream,會對文件加鎖。當你不考慮文件加鎖。當你需要讀取另一個文件的時候,會打開一個新的InputStream,kernel會分配另一個descriptor(文件流),一直累加,而一個進程的文件描述表是優先,到最后文件描述表將用被用完,所以為了以防萬一,最好每次使用完之后都加一個close()
。
測試代碼
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class TestInputStream {
public static void main(String[] args) {
// createTestFile();
// testInputStreamWithOutClose();
// testInputStreamWithOneFileWithOutClose();
testInputStreamWithClose();
}
/**
* 創建一萬個測試文件
*/
public static void createTestFile() {
try {
for (int i = 0; i < 10000; i++) {
FileOutputStream fos = new FileOutputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test" + i));
fos.write(i);
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用不同文件,不使用close,有"Too many open files in system"報錯
*/
public static void testInputStreamWithOutClose(){
try {
for (int i = 0; i < 10000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test" + i));
System.out.println(fis.toString() + " " + i);
// fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用同一個文件,不加close,有"Too many open files in system"報錯
*/
public static void testInputStreamWithOneFileWithOutClose(){
try {
for (int i = 0; i < 10000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1"));
System.out.println(fis.toString() + " " + i);
// fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 加close,無"Too many open files in system"報錯
*/
public static void testInputStreamWithClose(){
try {
for (int i = 0; i < 100000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1"));
System.out.println(fis.toString() + " " + i);
fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
當不使用close()方法時,會有報錯:Too many open files in system
java.io.FileInputStream@7e349a0e 6079
java.io.FileNotFoundException: /Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1 (Too many open files in system)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at TestInputStream.testInputStreamWithOneFileWithOutClose(TestInputStream.java:53)
at TestInputStream.main(TestInputStream.java:9)
InputStream的數據源是Internet連接時
這是我實際遇到的情況,實際業務情況為:我需要調用一個圖片存儲位置,用key讀到的圖片存到InputStream里面來進行進一步處理,而我忘記了對它進行close,經過測試,處理300左右的請求之后,就不能繼續請求了,這是為什么呢?是因為InputStream沒有被垃圾回收掉,還一直占用着連接,而圖片服務商有連接數限制,導致之后的請求沒有返回,被調用的InputStream類如下:
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.http.client.methods.CloseableHttpResponse;
public class ObjectInputStream extends FilterInputStream {
private CloseableHttpResponse httpResponse;
public ObjectInputStream(InputStream in, CloseableHttpResponse httpResponse) {
super(in);
this.httpResponse = httpResponse;
}
public void close() throws IOException {
this.httpResponse.close();
try {
super.close();
} catch (SocketException e) {
// expected from some implementations because the stream is closed
}
}
}
當沒有調用close方法的時候,沒有調用httpResponse.close();
,連接不會關掉,所以就尷尬了。
理論依據參考:http://stackoverflow.com/questions/26541513/why-is-it-good-to-close-an-inputstream