Every time you create either a FileInputStream or a FileOutputStream you are creating an object, even if you close it correctly and promptly, it will be put into a special category that only gets cleaned up when the garbage collector does a full GC.
就算自己 close 了也不是真的 close 了?還是得依賴 Full GC 呼叫 finalize 才會真的 close?有所謂的 a special category?
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
}
});
}
protected void finalize() throws IOException {
if ((fd != null) && (fd != FileDescriptor.in)) {
/* if fd is shared, the references in FileDescriptor
* will ensure that finalizer is only called when
* safe to do so. All references using the fd have
* become unreachable. We can call close()
*/
close();
}
}
如果他的意思是因為 override 了 finalize,所以會被排入 GC 時得呼叫 finalize,那也很奇怪, finalize 一直都在那啊?還是說他的意思是,finalize 裡寫的程式碼檢查會造成不必要的開銷?
去看看別的地方的 comments,也有人覺得這說法不太對。
這是文章的子討論串,你可以回到上層查看所有討論和文章
應該是你說的第二點,非 trivial finalize() 的 instance 會被丟到 finalize queue。 關於trivial finalize()1