1

Yes I know that InputStreams should be closed, but I wan't to know what happen if I don't and just drop the reference. Will the gc close it or it will hold the resource forever?

Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
  • finalize() close it, I found the answer: https://stackoverflow.com/a/33275951/1386911 – Daniel Hári Nov 20 '17 at 21:38
  • The answers here pretty much sum it up (why you need to close a FileInputStream). Especially the answer by chrylis (the last one) https://stackoverflow.com/questions/26541513/why-is-it-good-to-close-an-inputstream – Chetan Jadhav CD Nov 20 '17 at 21:40
  • Actually my question was: what happens. And I found the answer in FileInputStream's code, in finallise() method it is closed. Actually this answers a similar bug that presented in a system for years without causing a problem, this why I wanted to know how it has been survived: the gc closed the streams. – Daniel Hári Nov 20 '17 at 21:43
  • 1
    What happens? The file handle is held until the stream is GC'd. That may happen immediately, later, or never. In short, don't rely upon it happening. – Andy Turner Nov 20 '17 at 21:52
  • Yes that answers how a system can survive this kind of leaks, that is good to know, such bugs can be hidden for long time. – Daniel Hári Nov 20 '17 at 21:54

0 Answers0