teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

finalize

2018/06/26 04:02

投稿

退会済みユーザー
answer CHANGED
@@ -1,1 +1,29 @@
1
- GC(ガベコレ)が発生したときにとじられるやつですね(やってはいけないこと)
1
+ GC(ガベコレ)が発生したときにとじられるやつですね(やってはいけないこと)
2
+
3
+
4
+ # 追記
5
+
6
+ 参考資料としてほぼ同様の質問の回答
7
+ (今回とは InputStream がちがうので自分でさかのぼってみてください。大体は同じく finalize で閉じている )
8
+
9
+ [stackoverflow.com 引用](https://stackoverflow.com/questions/45456509/is-inline-created-inputstream-closed-automatically-by-gc)
10
+
11
+ ```
12
+ /**
13
+ * Ensures that the <code>close</code> method of this file input stream is
14
+ * called when there are no more references to it.
15
+ *
16
+ * @exception IOException if an I/O error occurs.
17
+ * @see java.io.FileInputStream#close()
18
+ */
19
+ protected void finalize() throws IOException {
20
+ if ((fd != null) && (fd != FileDescriptor.in)) {
21
+ /* if fd is shared, the references in FileDescriptor
22
+ * will ensure that finalizer is only called when
23
+ * safe to do so. All references using the fd have
24
+ * become unreachable. We can call close()
25
+ */
26
+ close();
27
+ }
28
+ }
29
+ ```