0
FileInputStream fis = new FileInputStream("test.txt");
fis.readAllBytes();

why does fis = null does not make this stream object ready for garbage collection why does it lead to memory leak why do we have to close the stream object

  • Garbage collection is not immediate. – Olivier Apr 25 '22 at 07:24
  • 1
    `fis = null` is a variable assignment. It’s not the purpose of variable assignments to close files. If you want to close the file, use the statement dedicated to close a file. Or better, use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). – Holger Apr 25 '22 at 10:02

0 Answers0